6

我想知道如何将 DataFrame 中的元素除以其最大行数。请参见以下代码:

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)

然后,df中的元素将根据同一行除以dfMax。有人有想法吗?

4

1 回答 1

7

我很确定你可以使用df.divide()

如果 df 是

                   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

和 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

然后df.divide(dfMax, axis=0)给你:

                   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
于 2013-06-25T09:36:42.207 回答