0

我有(df)如下数据框

address1            address2            Subject
NaN                 jesicca@gmail.com   Invoice 3
NaN                 NaN                 Invoice 4
rahul@gmail.com     shuan@gmail.com     Invoice 5

我的逻辑如下

  1. 如果 value 存在address1并且address2存在,则to_addressvalue 应该是 Address1

  2. 如果地址 1 和地址 2 中不存在任何值,或者两者都存在,NaN则应采用“david@gmailcom”

  3. 如果address1value 不存在或NaNvalueaddress2存在,则应采用此方法。

但是我的或逻辑代码不能按要求工作。我犯了什么错误。

我的代码:

for i, row in df.iterrows():
    subject  = row["Subject"]
    to_address = row['address1'] or row['address2'] or "david@gmailcom"
4

2 回答 2

1

尝试使用 lambda 函数:

df = pd.DataFrame([[float("nan"),"jesicca@gmail.com","Invoice 3"],[float("nan"),float("nan"),"Invoice 4"],
                   ["rahul@gmail.com","shuan@gmail.com","Invoice 5"]], columns = ["address1","address2","Subject"])
df["case"] = df.apply(lambda x: x["address1"] if not pd.isna(x["address1"]) \
                                else x['address2'] if not pd.isna(x["address2"]) \
                                else "david@gmailcom", axis = 1)
df

    address1        address2            Subject     case
0   NaN             jesicca@gmail.com   Invoice 3   jesicca@gmail.com
1   NaN             NaN                 Invoice 4   david@gmailcom
2   rahul@gmail.com shuan@gmail.com     Invoice 5   rahul@gmail.com
于 2020-07-22T10:32:23.783 回答
0

首先,你应该稍微简化你的逻辑(这个解释让我很困惑)。

第二,什么类型NaN?我认为它是float('nan'),但如果它只是字符串,NaN那么你需要更改float('nan')NaN

据我了解,是这样的:

for i, row in df.iterrows():
    subject  = row["Subject"]
    if row['address1'] == float('nan'):
        to_address = row['address1']
    elif row['address2'] == float('nan'):
        to_address = row['address2']
    else:
        to_address = "david@gmailcom"

因此,如果第一个地址是NaN,则检查第二个地址。如果那个也是NaN,则选择默认值。

于 2020-07-22T10:33:26.430 回答