Python世纪日期处理
Python century date process
我有以下世纪日期格式的数据:
date
0320316
NaN
1201102
我将字段导入为 str
。
我想要的是:
date date_new
0320316 19320316
NaN NaN
1201102 20201102
我试过了:
df["date_new"] = df[["date"]].apply(lambda x: (str((x[0:4].astype(int) + 1900)) + x[4:]) if (x.notnull() == True) else x, axis = 1)
然而,我得到了ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我错过了什么吗?
修复您的代码
df["date"].apply(lambda x: (str((int(x[0:3]) + 1900)) + x[3:]) if (pd.isna(x) == False) else x)
Out[47]:
0 19320316
1 NaN
2 20201102
Name: date, dtype: object
我有以下世纪日期格式的数据:
date
0320316
NaN
1201102
我将字段导入为 str
。
我想要的是:
date date_new
0320316 19320316
NaN NaN
1201102 20201102
我试过了:
df["date_new"] = df[["date"]].apply(lambda x: (str((x[0:4].astype(int) + 1900)) + x[4:]) if (x.notnull() == True) else x, axis = 1)
然而,我得到了ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我错过了什么吗?
修复您的代码
df["date"].apply(lambda x: (str((int(x[0:3]) + 1900)) + x[3:]) if (pd.isna(x) == False) else x)
Out[47]:
0 19320316
1 NaN
2 20201102
Name: date, dtype: object