简体   繁体   English

如何将多索引熊猫数据框的一个条目除以另一个

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

I have a multi-indexed pandas dataframe that looks like this (snippet): 我有一个多索引的熊猫数据框,看起来像这样(片段):

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

The output of df.index is (with the labels bit shortened for viewing purposes): 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'])

My question is, what is the best way to divide the a-tubulin data entry by the Smad3_pS423/425_customer entry? 我的问题是,将Smad3_pS423/425_customer a-tubulin数据条目除以Smad3_pS423/425_customer条目的最佳方法是什么?

One cumbersome method is: 一种麻烦的方法是:

    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

But this doesn't seem like the pandas way of doing this. 但这似乎不像pandas那样。 Does anybody know of an easier way to do this? 有人知道这样做更简单吗? (I suspect pandas might have built in a one liner to do this). (我怀疑pandas可能已经内置了一只班轮来做到这一点)。 Thanks 谢谢

How about just: 怎么样:

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

Here's the df dataframe I used, that you can load with df = pd.read_clipboard(sep=',', index_col=[0,1,2]) 这是我使用的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM