如何将多索引 pandas 数据帧的一个条目除以另一个条目

How to divide one entry of a multi-indexed pandas dataframe by another

我有一个多索引 pandas 数据框,看起来像这样(片段):

Smad3_pS423/425_customer 0    1        0.664263
                              2        0.209911
                              3        0.099809
                         5    1        0.059652
                              2        0.190174
                              3        0.138850
a-Tubulin                0    1        0.072436
                              2        0.068282
                              3        0.087989
                         5    1        0.083960
                              2        0.076102
                              3        0.068119

df.index 的输出是(为了便于查看缩短了 labels 位):

MultiIndex(levels=[[u'Customer_Col1A2', u'Smad2_pS465/467 customer', u'Smad3_pS423/425_customer', u'Smad4_customer', u'Smad7_customer', u'a-Tubulin'], [u'0', u'10', u'120', u'180', u'20', u'240', u'30', u'300', u'45', u'5', u'60', u'90'], [u'1', u'2', u'3']],
           labels=[[2, 2, 2, 2, 2, 2, 2, ... more_labels...]],
           names=[u'Antibody', u'Time', u'Repeats'])

我的问题是,将 a-tubulin 数据条目除以 Smad3_pS423/425_customer 条目的最佳方法是什么?

一个比较麻烦的方法是:

    ab=[]
    for i in self.data.index.get_level_values('Antibody'):
        ab.append(i)
    antibodies= list(set(ab))
    for i in antibodies:
        print self.data.loc[i]/self.HK

但这似乎不是 pandas 的做法。有人知道更简单的方法吗? (我怀疑 pandas 可能内置了一个衬里来做到这一点)。 谢谢

怎么样:

df.ix['a-Tubulin'] / df.ix['Smad3_pS423/425_customer']

            3
1 2          
0 1  0.109047
  2  0.325290
  3  0.881574
5 1  1.407497
  2  0.400170
  3  0.490594

这是我使用的 df 数据框,您可以使用 df = pd.read_clipboard(sep=',', index_col=[0,1,2])

加载它
0,1,2,3
Smad3_pS423/425_customer,0,1,0.664263
Smad3_pS423/425_customer,0,2,0.20991100000000001
Smad3_pS423/425_customer,0,3,0.09980900000000001
Smad3_pS423/425_customer,5,1,0.059652
Smad3_pS423/425_customer,5,2,0.190174
Smad3_pS423/425_customer,5,3,0.13885
a-Tubulin,0,1,0.072436
a-Tubulin,0,2,0.06828200000000001
a-Tubulin,0,3,0.087989
a-Tubulin,5,1,0.08396
a-Tubulin,5,2,0.076102
a-Tubulin,5,3,0.068119