Search code examples
pythonpandasloopsdataframehashable

how to iterate using iterrows() and check for a subset


I would find the rows in a dataframe which contains all of the elements of a tuple and then set a value in a specific column for the corrisponding index of the row

for ix, row in df.iterrows():
    if set(tuple).issubset(row):
      df.loc[ix, ['label']] = 1

but I get this error:

TypeError: unhashable type: 'list'

if I perform the following chunk of code it seems that it works but I don't know how to set the value in the label column where the row match the tuple

for row in df.astype(str).values.tolist():
  set(tuple).issubset(row))

does anyone have any suggestions?

Thanks for the help


Solution

  • Use enumerate and iloc.

    for idx, row in enumerate(df.astype(str).values.tolist()):
        if set(tuple).issubset(row):
            df.iloc[idx, df.columns.get_loc('label')] = 1