pandas .isin()用于列的每一行中的值列表 [英] Pandas .isin() for list of values in each row of a column

查看:145
本文介绍了 pandas .isin()用于列的每一行中的值列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题:我的DataFrame中有一列,该列具有多行,并且在每一行中,它包含1个或多个以'M'字母开头,后跟3位数字的值.如果值多于1,则用逗号隔开. 我想打印出DataFrame的视图,仅显示其中1列保存我指定的值的行(例如,我希望它们保存列表['M111','M222']中的任何项目. 我已经开始通过以下方式构建布尔掩码:

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']))]

在我看来,其中具有.split()方法的.apply()首先将列"值转换为每行中具有1个或多个值的列表,然后.isin()方法确认是否有任何每行项目列表中的项目总数位于指定值['M111','M222']的列表中. 但是在实践中,我没有得到所需的DataFrame视图,而是报错

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'

我在做什么错了?

亲切的问候, 格蕾姆

推荐答案

我认为您需要:

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

这篇关于 pandas .isin()用于列的每一行中的值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆