2 回答
TA贡献1784条经验 获得超8个赞
我猜你想要一个具有最大日期值的日期列max(date,date2)
df:
date age english date2 value
0 2020-01-08 29.0 55.0 NaN 5
1 2020-01-22 22.0 45.0 NaN 0
2 2020-01-08 29.0 55.0 2020-01-08 5
3 2020-01-18 43.0 75.0 2020-05-18 8
4 NaN NaN NaN 2019-02-11 3
df['date'] = pd.to_datetime(df['date'])
df['date2'] = pd.to_datetime(df['date2'])
df['date'] = df[['date','date2']].max(axis=1)
df.drop('date2', axis=1, inplace=True)
df.fillna(0,axis=1,inplace=True)
df:
date age english value
0 2020-01-08 29.0 55.0 5
1 2020-01-22 22.0 45.0 0
2 2020-01-08 29.0 55.0 5
3 2020-05-18 43.0 75.0 8
4 2019-02-11 0 0 3
编辑:
如果您只想替换为date2如果存在:
import numpy as np
df['date'] = pd.to_datetime(df['date'])
df['date2'] = pd.to_datetime(df['date2'])
df['date'] = np.where(df['date2'].isnull(),df['date'],df['date2'])
df.drop('date2', axis=1, inplace=True)
df.fillna(0,axis=1,inplace=True)
TA贡献1874条经验 获得超12个赞
import numpy as np df['date'] = np.where(df['date'].isna(), df['date2'], df['date']) df = df.drop('date2', axis=1) df = df.fillna(0)
添加回答
举报