pythonpandasdataframefilterisin

Assign value by filtering rows based on two conditions with loc and isin


I am trying to assign a value 'FL' to my column 'FL_PL' with the following code the value 'FL'is not assigned in that column. Nothing is assign. I only get a column of True and False in the output (not in the df).

size_types = ['small', 'micro']
pl_types = ['1', '11', '32']
df2.loc[(df2['SIZE'].isin(size_types)) &
        (df2['TYPE'].isin(pl_types)), 'PL_FL'] == 'FL'

Solution

  • You need to use = to assign the value, == is the equality operator:

    df2.loc[df2['SIZE'].isin(size_types) &
            df2['TYPE'].isin(pl_types), 'PL_FL'] = 'FL'