Filter Pandas by a Column with List Values

Given a pandas DataFrame that contains a column with list values

> pd.DataFrame.from_dict(
      {'name' : {0 : 'foo', 1: 'bar', 2: 'baz', 3: 'foz'}, 
       'Attributes': {0: ['x', 'y'], 1: ['y', 'z'], 2: ['x', 'z'], 3: []}
      })

   name    Attributes
0  foo     ['x', 'y']
1  bar     ['y', 'z']
2  baz     ['x', 'z']
3  foz     []

How can the DataFrame be filtered for only those rows don't contain a certain value, e.g. 'y', in the lists:

2  baz     ['x', 'z']
3  foz     []

Thank you in advance for your consideration and response.

Upvotes: 1

Views: 259

Answers (2)

Enrique Ortiz Casillas
Enrique Ortiz Casillas

Reputation: 577

This should work (although it's not very elegant)

def filter_data_frame(df):
    good_index = []
    for i in range(len(df)):
        if "y" not in df.iloc[i,1]:
            good_index.append(i)

return df.iloc[good_index, :]

Upvotes: 1

anky
anky

Reputation: 75080

you can convert the series of list to a dataframe and compare if all the columns are not equal to y:

# is they aren't actual list : df['Attributes'] = df['Attributes'].apply(ast.literal_eval)
df[pd.DataFrame(df['Attributes'].tolist()).ne('y').all(1)]

  Name Attributes
2  baz     [x, z]

If they are not actual lists:

df[df['Attributes'].str.count('y').eq(0)]

Upvotes: 4

Related Questions