我有两个数据框,其中大多数列都不同,但是其中两个列几乎相同,即帧和日期。
df_1
id FRAME var_1 date
1 10 15 3/4/16
2 12 69 3/5/17
df_2
id frame var_2 date_time
1 11 15 3/2/16 08:14:32
2 12 69 3/5/17 09:12:29
现在,我将pd.concat用作df_3 = pd.concat([df_1, df_2], axis=0, ignore_index=True)
df_3
id FRAME var_1 date frame var_2 date_time
1 10 15 3/4/16 NaN NaN NaT
2 12 69 3/5/17 NaN NaN NaT
3 NaN NaN NaT 11 15 3/2/16 08:14:32
4 Nan NaN NaT 12 69 3/5/17 09:12:29
我想将FRAME和date / date_time列合并
df_3
id FRAME var_1 var_2 date_time
1 10 15 NaN 3/4/16
2 12 69 NaN 3/5/17
3 11 NaN 15 3/2/16 08:14:32
4 12 NaN 69 3/5/17 09:12:29
答案 0 :(得分:2)
将pd.concat
与rename
一起使用:
df_3 = pd.concat([df_1,
df_2.rename(columns={'frame':'FRAME', 'date_time':'date'})],
ignore_index=True,
sort=True)
输出
FRAME date var_1 var_2
0 10 3/4/16 15.0 NaN
1 12 3/5/17 69.0 NaN
2 11 3/2/16 08:14:32 NaN 15.0
3 12 3/5/17 09:12:29 NaN 69.0