Divide pandas data elements by its max row

I wonder how to split elements in a DataFrame into a max string. See the following code:

index = pd.date_range('1/1/2000', periods=8) df = DataFrame(np.random.randn(8, 3), index=index, columns=['A', 'B', 'C']) dfMax = df.max(axis=1) 

and then the elements in df will be split into dfMax based on the same line. Does anyone have an idea?

+4
source share
1 answer

I'm sure you can just use df.divide()

If df is

  ABC 2000-01-01 -1.420930 -0.836832 0.941576 2000-01-02 -1.011576 0.297129 0.768809 2000-01-03 0.482838 0.331886 1.573922 2000-01-04 -1.359400 -0.909661 1.144215 2000-01-05 0.142007 -1.600080 2.160389 2000-01-06 -0.782341 0.452034 0.242853 2000-01-07 0.414489 -1.319712 -0.129439 2000-01-08 -0.817271 -1.073293 1.689901 

and dfMax:

 2000-01-01 0.941576 2000-01-02 0.768809 2000-01-03 1.573922 2000-01-04 1.144215 2000-01-05 2.160389 2000-01-06 0.452034 2000-01-07 0.414489 2000-01-08 1.689901 

Then df.divide(dfMax, axis=0) gives you:

  ABC 2000-01-01 -1.509098 -0.888757 1.000000 2000-01-02 -1.315771 0.386480 1.000000 2000-01-03 0.306774 0.210866 1.000000 2000-01-04 -1.188064 -0.795009 1.000000 2000-01-05 0.065732 -0.740644 1.000000 2000-01-06 -1.730712 1.000000 0.537245 2000-01-07 1.000000 -3.183953 -0.312285 2000-01-08 -0.483621 -0.635122 1.000000 
+4
source

Source: https://habr.com/ru/post/1488033/


All Articles