我的数据框看起来像-
var view_train = '<?=$this->render("_view")?>';
我希望数据框看起来像-
date age english date2 value
2020-01-08 29 55 NaT 5
2020-01-22 22 45 NaT 0
2020-01-08 29 55 2020-01-08 5
2020-01-18 43 75 2020-05-18 8
NaT NaN NaN 2019-02-11 3
如何在大熊猫中做
答案 0 :(得分:2)
我想您想要一个具有最大日期值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)
答案 1 :(得分:1)
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)