[英]How to select rows with specific string from a column with the type of list Python
I have a python dataframe df with several columns in it. 我有一个python dataframe df,其中有几列。 and I want to select rows with specif string from a column names A, and the data type in each cell of A is ['str1','str2','str3'...].
我想从名称为A的列中选择带有指定字符串的行,并且A的每个单元格中的数据类型为['str1','str2','str3'...]。 I used df['A'].str.contains('str2')] but it doesn't work.
我使用了df ['A']。str.contains('str2')],但它不起作用。 I don't know where is the problem.
我不知道问题出在哪里。 Should I convert list type to string before I can do filter?
我可以在进行过滤之前将列表类型转换为字符串吗?
example: 例:
dataframe: 数据帧:
A B C D
1 [animal,tools,new] white nyc 25
2 [Italian,restaurant,food] black boston 20
3 [Italian,animal,place] red chicago 5
4 [sky,temp,something] red island 90
I wanna choose the rows that only contain 'animal' in column A ideal output: 我想在A列理想输出中选择仅包含“动物”的行:
A B C D
1 [animal,tools,new] white nyc 25
2 [Italian,animal,place] red chicago 5
I think you can using in
我想你可以用
in
df=pd.DataFrame({'A':[['str1','str2','str3'],['str1']]})
['str2' in x for x in df.A]
Out[28]: [True, False]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.