或者当存在使用 Python 的 NaN 值时逻辑不工作
Or logic not working when there are NaN values using Python
我有数据框 (df)
如下所示
address1 address2 Subject
NaN jesicca@gmail.com Invoice 3
NaN NaN Invoice 4
rahul@gmail.com shuan@gmail.com Invoice 5
我的逻辑如下
如果存在 address1
的值并且存在 address2
,则 to_address
值应为 Address1
如果地址 1 和地址 2 或两者都没有值 NaN
,则应采用“david@gmailcom”
如果 address1
值不存在或存在 NaN
和 address2
值,则应采用此值。
但是我的或逻辑代码没有按要求工作。我犯了什么错误。
我的代码:
for i, row in df.iterrows():
subject = row["Subject"]
to_address = row['address1'] or row['address2'] or "david@gmailcom"
尝试使用 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
首先,你应该简化一下你的逻辑(这个解释让我很困惑)。
其次,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
则选择默认值。
我有数据框 (df)
如下所示
address1 address2 Subject
NaN jesicca@gmail.com Invoice 3
NaN NaN Invoice 4
rahul@gmail.com shuan@gmail.com Invoice 5
我的逻辑如下
如果存在
address1
的值并且存在address2
,则to_address
值应为 Address1如果地址 1 和地址 2 或两者都没有值
NaN
,则应采用“david@gmailcom”如果
address1
值不存在或存在NaN
和address2
值,则应采用此值。
但是我的或逻辑代码没有按要求工作。我犯了什么错误。
我的代码:
for i, row in df.iterrows():
subject = row["Subject"]
to_address = row['address1'] or row['address2'] or "david@gmailcom"
尝试使用 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
首先,你应该简化一下你的逻辑(这个解释让我很困惑)。
其次,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
则选择默认值。