If语句检查数据框列值的长度

  • 本文关键字:语句 数据 If python pandas
  • 更新时间 :
  • 英文 :


我正在尝试创建一个用户定义的函数来检查数据框列的长度并返回一定的值。

tbody> <<tr>
帐户开户日期 帐户开户日期
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 ''

但与上面的向量解

相比,这仍然是低效的

最新更新