我有一个多索引数据帧列表,例如:
df1 = pd.DataFrame(np.random.randn(4, 4),
index=[np.array(['bar', 'baz', 'foo', 'qux']), np.array(['one','one','one','one',])])
df2 = pd.DataFrame(np.random.randn(4, 4),
index=[np.array(['bar', 'baz', 'foo', 'qux']), np.array(['two','two','two','two'])])
l = [df1,df2]
df1
在哪里
0 1 2 3
bar one 0.027394 0.519734 -0.479718 -2.261858
baz one -0.259952 -1.790802 0.017535 -0.720666
foo one -0.756650 1.116440 -0.400762 -1.190532
qux one -0.845879 1.076155 -0.875078 1.529870
和df2
0 1 2 3
bar two -0.443989 1.031419 -0.303421 0.360927
baz two -0.646968 0.616669 0.380372 -0.828982
foo two -0.953993 -0.664360 -0.673810 0.569510
qux two -0.522093 -0.680101 0.303099 1.118680
假设所有数据帧中的0级索引和列名都相同,我想将它们结合起来得到类似的东西
0 1 2 3
bar one 0.027394 0.519734 -0.479718 -2.261858
two -0.443989 1.031419 -0.303421 0.360927
baz one -0.259952 -1.790802 0.017535 -0.720666
two -0.646968 0.616669 0.380372 -0.828982
foo one -0.756650 1.116440 -0.400762 -1.190532
two -0.953993 -0.664360 -0.673810 0.569510
qux one -0.845879 1.076155 -0.875078 1.529870
two -0.522093 -0.680101 0.303099 1.118680
我该怎么做?
答案 0 :(得分:1)
您可以使用concat
+ sort_index
df=pd.concat([df1,df2]).sort_index(level=0,axis=0)
df
Out[191]:
0 1 2 3
bar one -0.036748 1.505823 1.085209 -0.720105
two 0.063424 0.202523 1.324711 -1.088452
baz one -0.023442 -0.340350 1.303056 0.725405
two 0.608751 1.880995 0.001719 -0.133119
foo one -1.222408 -1.264111 -1.039589 0.387900
two 0.915225 0.700590 0.996271 0.161716
qux one 0.252766 2.542727 1.119359 -0.707420
two 0.529703 0.984748 0.086643 0.484503