Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

divide pandas dataframe elements by its line max

Tags:

pandas

divide

I wonder how to divide the elements in DataFrame by its line max. See 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 dividedby dfMax based on the same line. Does anyone have an idea?

like image 949
perigee Avatar asked Jun 25 '13 09:06

perigee


People also ask

How do you get the maximum values of each group in a pandas?

To get the maximum value of each group, you can directly apply the pandas max() function to the selected column(s) from the result of pandas groupby.

What is the max rows in pandas DataFrame?

Set Max Number of Rows The default number is 60. As shown, if we have a data frame with more than 60 rows, 50 rows in the middle will be truncated. If we set the option larger than the number of rows of our data frame, all the rows will be displayed.


1 Answers

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

If df is

                   A         B         C
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 is:

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:

                   A         B         C
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
like image 50
Matti John Avatar answered Oct 05 '22 11:10

Matti John