Pandas:使用条件和运算符(或、与)的Lambda函数。

huangapple go评论94阅读模式
英文:

Pandas: Lambda Function using a conditional as well as an operator (Or, and))

问题

我正在学习关于Lambda函数在pandas中的应用,我尝试根据是否在列(A)中出现特定单词来创建一个新列("Appeals","Audit","Assessment"),但是我收到了以下错误:

AttributeError: 'str'对象没有属性'str'

d = {"A": ["Assessment Division", "Division of Appeals", "Calgary Audit", "Halifax Market", "Georgian Markers"],
"B": [100, 300, 400, 500, 400]}

sample = pd.DataFrame(d)

sample["results"] = sample["A"].apply(lambda x: "Invoice" if "Audit" in x or "Appeals" in x or "Assessment" in x else "NoAction")

我也尝试了下面的代码来缩短我的代码,但是收到相同的错误:

d = {"A": ["Assessment Division", "Division of Appeals", "Calgary Audit", "Halifax Market", "Georgian Markers"],
"B": [100, 300, 400, 500, 400]}

sample = pd.DataFrame(d)

reqs = ["Audit", "Appeals", "Assessment"]

sample["results"] = sample["A"].apply(lambda x: "Invoice" if any(req in x for req in reqs) else "NoAction")
英文:

I am working through my comprehension of lambdas functions as it pertains to pandas.
I am trying to create a new column based on if a certain word appears in column (A) ("Appeals","Audit","Assessment")

However I am receiving an error of:

AttributeError: 'str' object has no attribute 'str'

d = {"A":["Assessment Division","Division of Appeals","Calgary Audit","Halifax Market","Georgian Markers"],
"B":[100,300,400,500,400]

sample = pd.DataFrame(d)

sample["results"] = sample["A"].apply(lambda x: "Invoice" if x.str.contains("Audit") or x.str.contains("Appeals") or x.str.contains("Assessment") else "NoAction")


I have also tried this to attempt to shorten my code but receive the same error:

d = {"A":["Assessment Division","Division of Appeals","Calgary Audit","Halifax Market","Georgian Markers"],
"B":[100,300,400,500,400]

sample = pd.DataFrame(d)

reqs = ["Audit","Appeals","Assessment"]

sample["results"] = sample["A"].apply(lambda x: "Invoice" if x.str.contains().isin(reqs) else "NoAction")

答案1

得分: 2

你可以使用 any() 来检查字符串是否包含reqs中的任何单词:

reqs = ["Audit","Appeals","Assessment"]

mask = sample['A'].apply(lambda x: any(r in x for r in reqs))
sample['results'] = np.where(mask, 'Invoice', 'NoAction')

print(sample)

打印结果:

                     A    B   results
0  Assessment Division  100   Invoice
1  Division of Appeals  300   Invoice
2        Calgary Audit  400   Invoice
3       Halifax Market  500  NoAction
4     Georgian Markers  400  NoAction

编辑:一行代码实现:

sample['results'] = sample['A'].apply(lambda x: 'Invoice' if any(r in x for r in reqs) else 'NoAction')
英文:

You can use any() to check if the string contains any word from your reqs:

reqs = ["Audit","Appeals","Assessment"]

mask = sample['A'].apply(lambda x: any(r in x for r in reqs))
sample['results'] = np.where(mask, 'Invoice', 'NoAction')

print(sample)

Prints:

                     A    B   results
0  Assessment Division  100   Invoice
1  Division of Appeals  300   Invoice
2        Calgary Audit  400   Invoice
3       Halifax Market  500  NoAction
4     Georgian Markers  400  NoAction

EDIT: One-liner:

sample['results'] = sample['A'].apply(lambda x: 'Invoice' if any(r in x for r in reqs) else 'NoAction')

huangapple
  • 本文由 发表于 2023年6月9日 03:51:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定