Skip to content Skip to sidebar Skip to footer

Pandas .isin() For List Of Values In Each Row Of A Column

I have a small problem: I have a column in my DataFrame, which has multiple rows, and in each row it holds either 1 or more values starting with 'M' letter followed by 3 digits. If

Solution 1:

I think you need:

df2 = df[df['Column'].str.contains('|'.join(['M111', 'M222']))]

Solution 2:

You can only access the isin() method with a Pandas object. But split() returns a list. Wrapping split() in a Series will work:

# sample data
data = {'Column':['M111, M000','M333, M444']}
df = pd.DataFrame(data)

print(df)
       Column
0  M111, M000
1  M333, M444

Now wrap split() in a Series. Note that isin() will return a list of boolean values, one for each element coming out of split(). You want to know "whether or not any of item in list...are in the list of specified values", so add any() to your apply function.

df[df['Column'].apply(lambda x: pd.Series(x.split(', ')).isin(['M111', 'M222']).any())]

Output:

       Column
0  M111, M000

As others have pointed out, there are simpler ways to go about achieving your end goal. But this is how to resolve the specific issue you're encountering with isin().

Post a Comment for "Pandas .isin() For List Of Values In Each Row Of A Column"