我在熊猫中有两个数据框
(从Spyder Variable Explorer复制)

df1

index   0    1    2    3    4    5   6
0       Loc 0.0  0.0  0.0  0.25 0.0  light
1       Loc 0.0  0.0  0.0  0.25 0.0  light
2       Loc 0.0  0.0  0.0  0.25 0.0  light
3       Loc 0.0  0.0  0.0  0.25 0.0  light


df2

index   0       1      2    3   4   5   6
0       DCos  -0.25  -0.2  0.9  nan nan nan
1       DCos  -0.25   0.2  0.9  nan nan nan
2       DCos   0.25  -0.2  0.9  nan nan nan
3       DCos   0.25   0.2  0.9  nan nan nan


我想将数据框2附加到数据框1,以具有

index   0    1    2    3    4    5   6      7       8     9    10   11  12  13
0       Loc 0.0  0.0  0.0  0.25 0.0  light  DCos  -0.25  -0.2  0.9  nan nan nan
1       Loc 0.0  0.0  0.0  0.25 0.0  light  DCos  -0.25   0.2  0.9  nan nan nan
2       Loc 0.0  0.0  0.0  0.25 0.0  light  DCos   0.25  -0.2  0.9  nan nan nan
3       Loc 0.0  0.0  0.0  0.25 0.0  light  DCos   0.25   0.2  0.9  nan nan nan


我试过了

df1.join.(df2)


但df1并未更改。我知道文档中描述了一个附加函数,但仅在附加行中。有没有一种方法可以追加列?

最佳答案

免责声明:我没有50代表意见。因此,此答案只是一个注释,因为EdChum提供了正确的答案。

您应该查看各种concat,合并和加入here的文档。

如果您尝试使用索引来连接两个数据帧,则只需要:

df3 = pd.concat([df1,df2], axis=1)


这会将第二个数据帧(df2)放在索引匹配的第一个数据帧(df1)旁边。

如果要连接而对索引不敏感,请尝试

df3 = pd.concat([df1,df2], axis=1, ignore_index=True)

关于python - 在 Pandas 中追加数据框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39352725/

10-09 01:00
查看更多