一列中有多个字符串匹配

2024-10-04 01:22:31 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,我正在使用isin()和pandas查找数据集“条件”列中包含“10”或“小时”的汽车。 我正在使用

UScars.isin({'condition':[10, 'hours']})

但这给了我一个系统错误

SystemError: 'built-in method view of numpy.ndarray object at 0x000001F40BF30EE0' returned a result with an error set.

我使用condition列上的split()将类型从str转换为list

我不知道我哪里出错了。任何帮助都将不胜感激

谢谢

enter image description here


Tags: 数据inpandas系统错误condition条件汽车
1条回答
网友
1楼 · 发布于 2024-10-04 01:22:31

您需要使用str.contains运算符来指定多个字符串匹配

pd.isin从文件中:

Whether each element in the DataFrame is contained in values.

df = pd.DataFrame({'A' : [0,1,2],
                  'Condition' : ['10 Hours', '20 Hours', '30 Days']})

print(df)

   A Condition
0  0  10 Hours
1  1  20 Hours
2  2   30 Days



df[df['Condition'].str.contains('10|Hours')]

   A Condition
0  0  10 Hours
1  1  20 Hours

相关问题 更多 >