gpt4 book ai didi

python - 如何获得两个 MultiIndex DataFrame 的并集?

转载 作者:太空宇宙 更新时间:2023-11-04 05:42:20 25 4
gpt4 key购买 nike

如何合并两个多索引数据帧?

例如,假设我有:

index1 = pd.MultiIndex.from_tuples([('2010-01-01', 'Jim'),
('2010-01-01', 'Mike'),
('2010-01-02', 'Sam')])
index2 = pd.MultiIndex.from_tuples([('2010-01-02', 'Jim'),
('2010-01-02', 'Sam'),
('2010-01-03', 'Joe')])
df1 = pd.DataFrame([[7,0,7],[4,3,2],[6,2,6]],
index=index1, columns=['a', 'b', 'c'])
df2 = pd.DataFrame([[4,2,0],[8,8,4],[5,5,3]],
index=index2, columns=['a', 'b', 'c'])

这导致:

>> df1
a b c
2010-01-01 Jim 7 0 7
Mike 4 3 2
2010-01-02 Sam 6 2 6

>> df2
a b c
2010-01-02 Jim 4 2 0
Sam 8 8 4
2010-01-03 Joe 5 5 3

我想合并 df1df2 生成:

>> df3
a1 b1 c1 a2 b2 c2
2010-01-01 Jim 7 0 7 NaN NaN NaN
Mike 4 3 2 NaN NaN NaN
2010-01-02 Jim NaN NaN NaN 4 2 0
Sam 6 2 6 8 8 4
2010-01-03 Joe NaN NaN NaN 5 5 3

我正在努力寻找一个好的方法来做到这一点。有什么建议吗?

最佳答案

尝试:

df3 = df1.join(df2, how='outer', lsuffix='1', rsuffix='2')

或者

df3 = pd.merge(df1, df2, how='outer', left_index=True, right_index=True)

关于python - 如何获得两个 MultiIndex DataFrame 的并集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33460209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com