检查pandas DataFrame列中的字符串中是否包含字符串

The*_*man 2 python dataframe pandas

我有一个非常简单的pandas DataFrame,我想选择DataFrame中在其中包含另一个字符串的列中具有数据的部分

因此,如果这是我的DataFrame并且我想要包含some在该Loc列中的那些列,该怎么做?

             Loc 
0      'something'  
1      'nothing'  
Run Code Online (Sandbox Code Playgroud)

我尝试了两件事:

df['some' in df['Loc']]
df[df.Loc.contains('some')]
Run Code Online (Sandbox Code Playgroud)

但是,两种解决方案都行不通。

Ale*_*der 5

您需要使用contains字符串访问器方法之一。

>>> df['Loc'].str.contains('some')
0     True
1    False
Name: Loc, dtype: bool
Run Code Online (Sandbox Code Playgroud)

然后,可以对结果使用布尔索引,以选择数据框的相关行。