我正在尝试创建一个用户定义的函数来检查数据框列的长度并返回一定的值。
帐户开户日期 | 帐户开户日期 | 27-04-2011 | 南 |
---|---|
南 | 南 |
南 | 01-01-2012 |
iuc,您需要:
s = pd.Series(np.where(df['Account Open Date'].notna(), 'OPEN', ''),
index=df.index)
df['Account Close Date'] = df['Account Close Date'].fillna(s)
输出:
Account Open Date Account Close Date
0 27-04-2011 OPEN
1 NaN
2 NaN 01-01-2012
对代码的修改如下:
def close_date(row):
close_date = row['Account Close Date']
open_date = row['Account Open Date']
if pd.notna(close_date):
return close_date
elif pd.notna(open_date):
return 'OPEN'
else:
return ''
但与上面的向量解
相比,这仍然是低效的