Skip to content Skip to sidebar Skip to footer

How To Normalize Multiindex Dataframe With Its Sum Values

I have the following CSV data: id,gene,celltype,stem,stem,stem,bcell,bcell,tcell id,gene,organs,bm,bm,fl,pt,pt,bm 134,foo,about_foo,20,10,11,23,22,79 222,bar,about_bar,17,13,55,12,

Solution 1:

You could

sum = result.sum(level=0, axis=1)

print(result.div(sum, axis=1, level=0))

to get

        bm                        fl               pt           
     bcell      stem     tcell bcell stem tcell bcell stem tcell
cell                                                            
foo      0  0.159574  0.840426     0    1     0     1    0     0
bar      0  0.145631  0.854369     0    1     0     1    0     0

Post a Comment for "How To Normalize Multiindex Dataframe With Its Sum Values"