6

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?

Ben
  • 51,770
  • 36
  • 127
  • 149
perigee
  • 9,438
  • 11
  • 31
  • 35

1 Answers1

7

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
Matti John
  • 19,329
  • 7
  • 41
  • 39
  • Thx a lot, another question, in case the elements are integers, how can i convert them to float, tried astype, but doesn't work – perigee Jun 25 '13 at 09:53
  • Still have one question, how can I deal with the case that dfMax contains zeros. Since the data in dfDiv = df.divide() will contain infinity...thx in advance – perigee Jun 25 '13 at 10:08
  • never mind again :-D, solved by: dfDiv[np.isinf(dfDiv)] = np.nan – perigee Jun 25 '13 at 10:14