Greem666 Greem666 - 1 year ago 185
Python Question

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 there is more than 1 value, they are separated by a comma.
I would like to print out a view of the DataFrame, only featuring rows where that 1 column holds values I specify (e.g. I want them to hold any item from list ['M111', 'M222'].
I have started to build my boolean mask in the following way:

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


In my mind, .apply() with .split() methods in there first convert 'Column' values to lists in each row with 1 or more values in it, and then .isin() method confirms whether or not any of items in list of items in each row are in the list of specified values ['M111', 'M222'].
In practice however, instead of getting a desired view of DataFrame, I get error

'TypeError: unhashable type: 'list'


What am I doing wrong?

Kind regards,
Greem

Answer Source

I think you need:

df2 = df[df['Column'].str.contains('|'.join(['M111', 'M222']))]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download