0

I'm using pandas to read a spreadsheet and then using iteration to split it up, along with a few other operations. But then, when I try to use the list.remove() method on one of the resulting dataframes (even after turning it into a list), it throws the following:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 

Here's the full code:

def correlationTester2(startSample, minSample):
    df = list(pd.read_csv('C:/Users/iijof/Desktop/sheet.csv').values)
    df = random.sample(df, len(df))
    dfTry = []
    dfTest = []
    counter = {}
    for i in df:
        if counter.get(i[9], 0) < startSample:
            dfTry.append(i)
            counter[i[9]] = counter.get(i[9], 0) + 1
        else:
            dfTest.append(i)
            counter[i[9]] = counter.get(i[9], 0) + 1

    tryCopy = list(dfTry)
    testCopy = list(dfTest)        
    for i in counter.keys():
        if counter[i] < minSample:
            for name in tryCopy:
                if name[9] == i:
                    dfTry.remove(name)
            for name in testCopy:
                if name[9] == i:
                    dfTest.remove(name)

    return dfTry, dfTest, counter

dfTry, dfTest, counter = correlationTester2(50,100)

The error occurs when runnings through the dfTry.remove(name) line. How do I fix it?

Let me know if I need to provide info about the structure of the spreadsheet, thanks.

Full Error Trace:

ValueError                                Traceback (most recent call last)
<ipython-input-99-de5890bffa67> in <module>()
     31     return dfTry, dfTest, counter
     32 
---> 33 dfTry, dfTest, counter = correlationTester2(50,100)
     34 
<ipython-input-99-de5890bffa67> in correlationTester2(startSample, minSample)
     24             for name in tryCopy:
     25                 if name[9] == i:
---> 26                     dfTry.remove(name)
     27             for name in testCopy:
     28                 if name[9] == i:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 
IanJ
  • 53
  • 6
  • 1
    Can you include a full error trace, please? – Kacperito Jan 11 '20 at 21:33
  • 1
    Have you done any research? This exact error has come up many times on here already. – AMC Jan 11 '20 at 21:46
  • Does this answer your question? [ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()](https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous) – AMC Jan 11 '20 at 21:47
  • Also your code is **extremely unidiomatic**, be careful. – AMC Jan 11 '20 at 21:47

0 Answers0