簡體   English   中英

在熊貓中追加數據框

[英]Append dataframes in pandas

我在熊貓中有兩個數據框(從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的文檔,在此處進行合並和連接。

如果您嘗試使用索引來連接兩個數據幀,則只需要:

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

這會將第二個數據幀(df2)放在索引匹配的第一個數據幀(df1)旁邊。

如果要串聯而對索引不敏感,請嘗試

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM