簡體   English   中英

如何使用索引合並熊貓中的多個數據框列?

[英]How to merge multiple data frame columns in pandas by using index?

我有六個熊貓數據框,如下所示。 我想通過使用 id 列(即索引)來加入它們。 在下文中,我提供了一個包含三個數據框的示例。

df1 = 
id              cnt1
A000        10
A001        20
A002        30
A010        10
A050        55
...........................
A317        20

df2 = 
id          cnt2
A000        10
A010        20
...........................
A316        20

df3 = 
id          cnt3
A010        20
................................
A318        20

加入后,我需要一個如下所示的數據框。

all_df =
id              cnt1    cnt2    cnt3
A000            10      10      0   
A001            20      0       0
A002            30      0       0
A010            10      20      20
A050            55      0       0
............................................................................
A316            0       20      0
A317            20      0       0
A318            0       0       20

請讓我知道該怎么做。 提前致謝。

IIUC,您希望pd.concat在列上

all_df = pd.concat([df1, df2], axis=1).fillna(0)
print(all_df)

      cnt1  cnt2
id
A000  10.0  10.0
A001  20.0   0.0
A002  30.0   0.0
A010  10.0  20.0
A050  55.0   0.0
A317  20.0   0.0
A316   0.0  20.0

暫無
暫無

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

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