我有两个数据字段(df_A和df_B)要合并为一个数据字段(df_total),但我只有NaN!该怎么办?
print(df_A)
DATE OILd GOLD US-RUSS
2000-01-31 0.073370 -0.026818 0.000099
2000-02-29 0.105606 0.038686 0.165135
2000-03-31 -0.121361 -0.061565 -0.065931
2000-04-30 -0.042815 -0.000181 -0.060177
print(df_B)
OILm COMM
DATE
2000-01-31 0.078671 0.037987
2000-02-29 0.020197 0.008202
2000-03-31 -0.133824 -0.059894
2000-04-30 0.108403 0.066403
df_total=df_A+df_B
print(df_total)
COMM GOLD OILd OILm US-RUSS
2000-01-31 NaN NaN NaN NaN NaN
2000-02-29 NaN NaN NaN NaN NaN
2000-03-31 NaN NaN NaN NaN NaN
2000-04-30 NaN NaN NaN NaN NaN
答案 0 :(得分:1)
您可以将pd.concat
与参数axis=1
一起使用
代码:
import pandas as pd
df1 = pd.DataFrame([['a', 1], ['b', 2]],
columns=['A', 'B'])
df2 = pd.DataFrame([['c', 3], ['d', 4]],
columns=['C', 'D'])
print(pd.concat([df1, df2], axis=1))
df1:
A B
0 a 1
1 b 2
df2:
C D
0 c 3
1 d 4
输出:
A B C D
0 a 1 c 3
1 b 2 d 4
所以在您的情况下,您必须像这样写
df_total = pd.concat([df_A, df_B], axis=1)
您可以检查Pandas documentation here for concat method
您也可以使用合并
print(df_A)
DATE OILd GOLD US-RUSS
2000-01-31 0.073370 -0.026818 0.000099
2000-02-29 0.105606 0.038686 0.165135
2000-03-31 -0.121361 -0.061565 -0.065931
2000-04-30 -0.042815 -0.000181 -0.060177
print(df_B)
OILm COMM
DATE
2000-01-31 0.078671 0.037987
2000-02-29 0.020197 0.008202
2000-03-31 -0.133824 -0.059894
2000-04-30 0.108403 0.066403
在两个df中,您都有DATE
列
print(df_A.merge(df_B, on='DATE', how='outer'))
会给你想要的结果