【发布时间】:2022-11-13 13:34:07
【问题描述】:
我有以下数据框(df)。
| GovKeepSecure | BankKeepSecure | OtherKeepSecure | Secure |
|---|---|---|---|
| Yes | Yes | Yes | Yes |
| No | No | Yes | No |
| No | Neutral | Yes | Neutral |
我正在寻找一个 python 函数来评估前 3 列,并返回在“安全”/第 4 列中出现超过 2 次的值。
例如,如果前 3 列(在同一行)中有 2 个或更多“否”,则“安全”列中的值会导致“否”。如果不满足此类条件,则“安全”列默认为“中性”。
我想知道我们将如何创建这样一个函数。
这是我正在尝试开发的方法。
import pandas as pd
def secure(row):
if row["GovKeepSecure", "BankKeepSecure", OtherKeepSecure] == ["Yes", "Yes", "Yes"]:
return "Yes"
if row["GovKeepSecure", "BankKeepSecure", OtherKeepSecure] == ["Yes", "Yes", "No"]:
return "Yes"
-------------------------------------------------------------------------------------(etc.)
df["Secure"] = df.apply(lambda row: secure(row), axis=1)
如果有更好的方法,请告诉我。非常感谢!
【问题讨论】: