包含字符串的熊猫DF选择列

时间:2019-12-10 19:18:24

标签: pandas dataframe

我目前正在尝试找出df [df ['A']。str.contains(“ hello”)]的反函数。我想从“ A”列返回所有行,如果它们在“ hello”中有一个字符串。

如果列“ A”中的值为“ he”,“ llo”,“ l”,则它们将返回true。

如果“ A”列中的值为“ het”,“ lil”,“ elp”,则它们将返回false。

是否有一种方法可以在数据帧中执行此操作而不迭代数据帧中的每一行?

由于使用ESRI ArcGIS 10.4软件限制,当前使用2.7。

1 个答案:

答案 0 :(得分:2)

您可以使用apply()的Pandas遍历A列的每一行,并评估它是否是'hello'的子字符串

def hello_check(row):
    return (row in 'hello')

df['contains_hello'] = df['A'].apply(hello_check)