Python:pandas apply 与 map

标签 python pandas apply

我很难理解 df.apply() 究竟是如何工作的。

我的问题如下:我有一个数据框df。现在我想在几个列中搜索某些字符串。如果在任何列中找到该字符串,我想为找到该字符串的每一行添加一个“标签”(在新列中)。

我可以用 mapapplymap 解决问题(见下文)。

但是,我希望更好的解决方案是使用 apply,因为它将函数应用于整个列。

问题:使用apply是不可能的吗?我的错误在哪里?

这是我使用mapapplymap 的解决方案。

df = pd.DataFrame([list("ABCDZ"),list("EAGHY"), list("IJKLA")], columns = ["h1","h2","h3","h4", "h5"])

使用map的解决方案

def setlabel_func(column):
    return df[column].str.contains("A")

mask = sum(map(setlabel_func, ["h1","h5"]))
df.ix[mask==1,"New Column"] = "Label"

使用applymap的解决方案

mask = df[["h1","h5"]].applymap(lambda el: True if re.match("A",el) else False).T.any()
df.ix[mask == True, "New Column"] = "Label"

对于 apply,我不知道如何将两列传递给函数/或者可能根本不理解其中的机制 ;-)

def setlabel_func(column):
    return df[column].str.contains("A")

df.apply(setlabel_func(["h1","h5"]),axis = 1)

上面给了我警报。

'DataFrame' object has no attribute 'str'

有什么建议吗?请注意,我的实际应用程序中的搜索功能更复杂,需要正则表达式功能,这就是我首先使用 .str.contain 的原因。

最佳答案

另一种解决方案是使用 DataFrame.any每行至少获得一个 True:

print (df[['h1', 'h5']].apply(lambda x: x.str.contains('A')))
      h1     h5
0   True  False
1  False  False
2  False   True

print (df[['h1', 'h5']].apply(lambda x: x.str.contains('A')).any(1))
0     True
1    False
2     True
dtype: bool

df['new'] = np.where(df[['h1','h5']].apply(lambda x: x.str.contains('A')).any(1),
                     'Label', '')

print (df)
  h1 h2 h3 h4 h5    new
0  A  B  C  D  Z  Label
1  E  A  G  H  Y       
2  I  J  K  L  A  Label

mask = df[['h1', 'h5']].apply(lambda x: x.str.contains('A')).any(1)
df.loc[mask, 'New'] = 'Label'
print (df)
  h1 h2 h3 h4 h5    New
0  A  B  C  D  Z  Label
1  E  A  G  H  Y    NaN
2  I  J  K  L  A  Label

关于Python:pandas apply 与 map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42175526/

相关文章:

python - 使用python诱变剂后,未显示元数据

javascript - node.js 中的 require() 是如何工作的?

python - 高级 Pandas : Create Multi-Index panda dataframe from certain row indices

python - 使用不同的 Python 工具连接 C/C++

python - key 错误 : 'PATH_INFO' when using Pydev and Google App Engine

python - 如何将两个二维数据框合并为一个多索引多维 Pandas 数据框?

python - 将 HTML 标签删除应用于 Pandas 列

python - 在 Pandas 中编码列标签以进行机器学习

python - 如何使用一个带有 lambda 的表达式合并两个计数值?

r - 将函数应用于列的可变子集