【发布时间】:2018-11-16 17:29:34
【问题描述】:
我有一个看起来像这样的 pandas DataFrame:
molecule species
0 a [dog]
1 b [horse, pig]
2 c [cat, dog]
3 d [cat, horse, pig]
4 e [chicken, pig]
我喜欢提取一个只包含那些行的 DataFrame,这些行包含selection = ['cat', 'dog'] 中的任何一个。所以结果应该是这样的:
molecule species
0 a [dog]
1 c [cat, dog]
2 d [cat, horse, pig]
最简单的方法是什么?
用于测试:
selection = ['cat', 'dog']
df = pd.DataFrame({'molecule': ['a','b','c','d','e'], 'species' : [['dog'], ['horse','pig'],['cat', 'dog'], ['cat','horse','pig'], ['chicken','pig']]})
【问题讨论】:
-
使用
df = df.loc[df.species.str.contains('cat|dog'),:]