简体   繁体   English

如何从列表类型为Python的列中选择具有特定字符串的行

[英]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.

相关问题 如何删除 Python 字符串列表的特定列? - How to remove specific column of Python string list? 如何从python中的列表中选择特定值 - How to select a specific value from a list in python Python根据列索引列表从枚举列表中选择特定的列值 - Python select specific column values from enumeration list based on column index list 如何从熊猫数据框中选择特定的列项目作为列表? - How to select specific column items as list from pandas dataframe? 如何在 pandas 中使用特定字符串模式的 select 行? - How to select rows with specific string patterns in pandas? 如何使用 python 中的列中的特定值进行 select 观察 - How to select observations with specific values from a column in python 将 pandas 库用于 Python,这是如何从特定行中获取 select 特定值,其中列是特定值? - Using pandas library for Python, is this how to select specific values from specific rows where columns are a specific value? Python:Select 来自 dataframe 的随机行,其中特定列的总和等于 X - Python: Select random rows from a dataframe for which the sum of a specific column equals X 如何平均每5行特定列并从Pandas的另一列中选择最后一个数据 - How can I average every 5 rows specific column and select last data from another column in Pandas 如何从 python 的列表中 select 特定项目? - How do I select specific items from a list in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM