我有一个非常简单的 pandas DataFrame,我想选择 DataFrame 中包含数据的列中包含另一个字符串的部分
因此,如果这是我的 DataFrame,并且我希望那些在 Loc
列中包含 some
的列,该怎么做?
Loc
0 'something'
1 'nothing'
我尝试了两件事:
df['some' in df['Loc']]
df[df.Loc.contains('some')]
但这两种解决方案都不起作用。
最佳答案
您需要使用 contains
,这是字符串访问器方法之一。
>>> df['Loc'].str.contains('some')
0 True
1 False
Name: Loc, dtype: bool
然后可以对结果使用 bool 索引来选择数据框的相关行。
关于python - 检查 pandas DataFrame 列中的字符串是否包含字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48634528/