我有两个带有MultiIndexes的数据文件,我需要正确地加入它们,然后在没有关键匹配的右侧获取项目。
how to do right join where key is null in python pandas的首选答案是这样做:
In [1]: dfa = pd.DataFrame({'A': range(5)}, index=range(5))
In [2]: dfb = pd.DataFrame({'A': range(10, 15)}, index=range(3,8))
In [3]: dfa
Out[3]:
A
0 0
1 1
2 2
3 3
4 4
In [4]: dfb
Out[4]:
A
3 10
4 11
5 12
6 13
7 14
In [5]: dfb.loc[dfb.index - dfa.index]
Out[5]:
A
5 12
6 13
7 14但是当我尝试使用我的代码时,我会得到一个错误:
TypeError: cannot perform __sub__ with this index type: <class 'pandas.core.indexes.multi.MultiIndex'>我给出了哪些选项,不能互相减去MultiIndexes?
发布于 2017-12-08 19:20:13
您不需要一个联接来获得不匹配的项。一个更简单的方法是使用以下方法:
df = dfb[dfb.index.isin(dfa.index) == False]使用联接的另一种方法是:
a = dfa.join(dfb, lsuffix='a', how='right')
a[a.Aa.isnull()][['A']]我不知道你的确切要求,但为了得到结果,第一可能是一个更清晰的方式来实现目标。问候
https://stackoverflow.com/questions/47720745
复制相似问题