我一直在尝试将两个MultiIndex连接在一起,但由于某种原因,它还没有成功……
import numpy as np
import pandas as pd
df = pd.DataFrame([[1,1,0,0,4],
[1,1,1,0,8],
[1,1,2,0,6],
[2,1,0,0,4],
[2,1,1,0,3]], columns=['a', 'b', 'c', 'd', 'e']
df2 = pd.DataFrame([[1,1,0,2,4],
[2,1,1,2,3]], columns=['a', 'b', 'c', 'd', 'e']
df = df.set_index(['a', 'b', 'c'])
df2 = df2.set_index(['a', 'b', 'c'])
df = pd.concat([df,df2], axis=1, join='inner')这就是我尝试的方式,我真的认为这应该行得通。是否有人可以帮助找出如何组合这两个,以便只获得列a,b和c匹配的行。
我想要的结果是:
d_x e_x d_y e_y
a b c
1 1 0 0 4 2 4
2 1 1 0 3 2 3发布于 2020-03-31 17:03:05
使用merge而不是concatenate
df.merge(df2, left_index=True, right_index=True)https://stackoverflow.com/questions/60946787
复制相似问题