Python 有没有办法将多索引数据帧与单个索引数据帧分开?

Python 有没有办法将多索引数据帧与单个索引数据帧分开?,python,pandas,dataframe,division,multi-index,Python,Pandas,Dataframe,Division,Multi Index,我有一个多索引系列,看起来像: Value1 Value2 Month Group Type 02 A Blue 2 3 Red 5 4 B Blue 4 7 Red 8 12 03 A Blue

我有一个多索引系列,看起来像:

                        Value1    Value2
Month  Group  Type
 02      A     Blue       2         3
               Red        5         4
         B     Blue       4         7
               Red        8         12

 03      A     Blue       9         22
               Red        44         5
         B     Blue       45         34
               Red        22         14
我想在这里将这个数据帧与另一个数据帧分开:

        Value
Month     
 02       2
 03       10
并根据月份指数进行划分。结果应该是这样的:

                        Value1    Value2
Month  Group  Type
 02      A     Blue       1         1.5
               Red        2.5         2
         B     Blue       2         3.5
               Red        4         6

 03      A     Blue       0.9         2.2
               Red        4.4         0.5
         B     Blue       4.5         3.4
               Red        2.2         1.4

我已经尝试了df.div(df2,level=0),但是得到了一个带有NaN的数据帧

您需要选择列值,用于除以系列,并添加
轴=0
,用于按索引进行比较:

df = df.div(df2['Value'], level=0, axis=0)
print (df)
                  Value1  Value2
Month Group Type                
2     A     Blue     1.0     1.5
            Red      2.5     2.0
      B     Blue     2.0     3.5
            Red      4.0     6.0
3     A     Blue     0.9     2.2
            Red      4.4     0.5
      B     Blue     4.5     3.4
            Red      2.2     1.4

您需要选择列
进行除以
系列
并添加
轴=0
进行索引比较:

df = df.div(df2['Value'], level=0, axis=0)
print (df)
                  Value1  Value2
Month Group Type                
2     A     Blue     1.0     1.5
            Red      2.5     2.0
      B     Blue     2.0     3.5
            Red      4.0     6.0
3     A     Blue     0.9     2.2
            Red      4.4     0.5
      B     Blue     4.5     3.4
            Red      2.2     1.4