divide pandas dataframe elements by its line max

耗尽温柔 提交于 2019-12-07 06:59:46

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/17293624/divide-pandas-dataframe-elements-by-its-line-max

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!