简体   繁体   中英

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):

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?

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. Does anybody know of an easier way to do this? (I suspect pandas might have built in a one liner to do this). 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])

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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