pythontuplesunsupportedoperation

Add column names to list given criteria


I'm trying to add column names (in form of tuples) from a dataframe to a list given a certain criteria. The criteria is simply if the data under each column is 1 (by itself, not as part of a string or a float). This is what I usually use, and it works, but only for smaller data sets (when scaled up, it renders a TypeError -- unsupported operand type(s) for +: 'int' and 'tuple'):

peaks_to_delete = []
for col in df3.columns:   #dataframe is df3
    if sum(df3[col]) == 1:
        peaks_to_delete.append(col)

Column names look like (125.98617542491242, 14.707909313725589) (332.3316802978516, 14.558341666666749) and so on...

I understand the error but don't get why the code works on smaller datasets and not larger ones.

Thanks in advance!


Solution

  • This most likely has nothing to do with the number of columns you run it against. It sounds like you have some value in one of the last columns that contains one or more tuples where the rest are numbers. You can run a for loop over the columns to see which don't sum

    for col in df.columns: 
        try: 
            x=sum(df[col])
        except:
            print(col)