Home > OS >  How to select rows where object is contained in cell's list
How to select rows where object is contained in cell's list

Time:01-26

Here's a MWE of my problem:

import pandas as pd
d = {"A": [[1,2,3],[2,3]], "B": ["Hey", "Test"]}
df = pd.DataFrame.from_dict(d)
df.loc[1 in df["A"]]

throws

KeyError: 'True: boolean label can not be used without a boolean index'

I tried a function like so:

t = lambda x, y: y in x 
df.loc[t(df["A"],1)]

but the error persists. How can use in operator, or actually any operator that is not comparing two strings? I have a feeling the issue is that I am using lists in my cells and pandas doesn't like me doing it, but I don't know, I'm not super experienced with the library.

CodePudding user response:

You need to use apply to mimick the expected behaviour:

df[df['A'].apply(lambda x: 1 in x)]

output:

           A    B
0  [1, 2, 3]  Hey

CodePudding user response:

Another option could be to explode "A" and use groupby_any to see if any matches appear:

num = 1
msk = df['A'].explode().eq(num).groupby(level=0).any()
out = df[msk]

Output:

           A    B
0  [1, 2, 3]  Hey

CodePudding user response:

You can select the entire first row by using df.iloc[0]: which yields

A    [1, 2, 3]
B          Hey
Name: 0, dtype: object

You can select the entire first column by using df['A']
which yields:

0    [1, 2, 3]
1       [2, 3]
Name: A, dtype: object  

You can select just the first row of the 'A' column by df['A'].iloc[0] which yields:

[1, 2, 3]  

You can then slice the first row Col A entry to get the first value by df['A'].iloc[0][0]
which yields:

1
  •  Tags:  
  • Related