且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 pandas 中将两个序列与MultiIndex相乘

更新时间:2023-11-29 19:20:22

尝试一下. reset_index删除最后一个级别,因此乘以它们是相同的

Try this. reset_index removes the last level, so they are the same when you multiply

In [25]: x = data_3levels.reset_index(level=2,drop=True)*data_2levels

由于您想要原始索引(并且形状未更改),因此可以正常工作.

Since you want the original index (and the shape hasn't changed), this works.

In [26]: x.index=data_3levels.index

In [27]: x
Out[27]: 
l1  l2   l3  
0   100  1000    2
         1001    2
         1002    2
1   101  1001    3
dtype: int64