Python 3.x 熊猫多指标多维交会
我有两个熊猫系列对象,它们都是通用多索引DF的子集。Python 3.x 熊猫多指标多维交会,python-3.x,pandas,numpy,Python 3.x,Pandas,Numpy,我有两个熊猫系列对象,它们都是通用多索引DF的子集。 例如: // SeriesA = Session Movie # Point in Time Session A mov1 1932 0.300000 1934 0.133333 1936 0.166667 1938
例如:
// SeriesA =
Session Movie # Point in Time
Session A mov1 1932 0.300000
1934 0.133333
1936 0.166667
1938 0.316667
// SeriesB =
Session Movie # Point in Time
Session A mov1 1932 0.300000
1934 0.133333
1940 0.200000
1942 0.083333
1944 0.133333
Session B mov1 1932 0.500000
我试图提取三个索引级别同时存在于SeriesA和serieb中的行。因此,对于上面的示例数据,正确的结果是
expected = [('Session A', 'mov1', 1932), ('Session A', 'mov1', 1934)]
我尝试使用numy的intersect1d()和pandas的intersection()函数,但两者都返回一个元组列表,其中每个元组都是其中一个索引标签的所有现有选项,即
result = [('Session A', Session B'), ('mov1'), (1932, 1934, 1936, 1938, 1940, 1942, 1944)]
我可以逐行迭代,但这似乎非常浪费。有什么我不知道的神奇解决方案吗?我得到了正确的输出:
与合并的另一个想法是:
c = ['Session','Movie #','Point in Time']
a = (SeriesA.reset_index()
.merge(SeriesB.reset_index(), on=c)
.set_index(c)
.index
.tolist())
print (a)
[('Session A', 'mov1', 1932), ('Session A', 'mov1', 1934)]
我得到了正确的输出:
与合并的另一个想法是:
c = ['Session','Movie #','Point in Time']
a = (SeriesA.reset_index()
.merge(SeriesB.reset_index(), on=c)
.set_index(c)
.index
.tolist())
print (a)
[('Session A', 'mov1', 1932), ('Session A', 'mov1', 1934)]
谢谢把它扔到一个小阵营里给了我我需要的汉克斯!把它扔到一个小天体阵列上给了我所需要的