![](/img/trans.png)
[英]Collect cells in pandas df that are listed in another pandas df (with same index)
[英]Concatenating 3 df with the same index in pandas
我在連接具有相同索引的3個數據幀時遇到問題(在這種情況下,索引是日期)。 我試圖將三個differnetnet數據框繪制到一個可繪制的圖上,並且將這些數據框組合起來將是最簡單的。 目前,我正在使用如下代碼。
pd.concat([df1,df2,df3], axis = 1)
但我提出以下錯誤。
ValueError: cannot reindex from a
duplicate axis
我的數據框的結構如下。
A
7/28/2018 4
7/29/2018 5
B
7/28/2018 3
7/29/2018 4
C
7/28/2018 1
7/29/2018 2
我想要以下結果:
A B C
7/28/2018 4 3 1
7/29/2018 5 4 2
有人可以在這里解釋我的錯誤做法嗎?
你應該確保你的指數是完全相同的。 這是一個最小的示例,它顯示您可以使用重復的索引進行連接,只要它們是相同的:
idx = ['7/28/2018', '7/28/2018', '7/29/2018']
df_A = pd.DataFrame({'A': [1, 2, 3]}, index=idx)
df_B = pd.DataFrame({'B': [4, 5, 6]}, index=idx)
df_C = pd.DataFrame({'C': [7, 8, 9]}, index=idx)
res = pd.concat([df_A, df_B, df_C], axis=1)
print(res)
A B C
7/28/2018 1 4 7
7/28/2018 2 5 8
7/29/2018 3 6 9
即使重新排序其中一個索引也將導致此中斷。 另一種解決方案是在連接之前刪除重復的索引,請參閱刪除具有重復索引的行 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.