【发布时间】:2021-10-18 09:35:37
【问题描述】:
我想检查列表中的项目是否在我的 DF 的列中。
简单明了的基础知识:
fruit = ['apple','banana'] # This items should be in the column
fruit = ', '.join(fruit) # Think this is the point where it goes wrong...
fruit_resulst = df['all_fruit'].str.contains(fruit) # Check if column contains fruit
df_new = df[fruit_resulst] # Filter so that we only keep the TRUEs
这可行,但不完全。它仅按此特定顺序工作,但我希望它在所有订单中工作(例如,如果列行包含列表中的所有项目,那么我想保留它们。否则,删除。
df['all_fruit']
Apple, Banana #Return! Because it contains apple and banana
Banana # Do not return
Banana, Apple #Return! Because it contains apple and banana
Apple # Do not return
Apple, Banana, Peer #Return! Because it contains apple and banana
提前非常感谢!
【问题讨论】:
标签: python pandas string dataframe