Pandas.DataFrame按行求百分数(比例数)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a19990412/article/details/90745056

简述

Motivation

  • 一般来说,每个部分的内容数量是较为容易获取的,但比例(百分数)这样的数据是二次数据,这样的操作很常见
  • 比例的信息相比于纯粹的数字更体现的整体体系的内部变化迁移的过程

Contribution

  • 给了实例,follow下就没问题了~

Codes

  • 导入包的部分,我就不写了哈

这里假设每行是属于不同月份的数据

>>> df
         a   b   c   d   e
month0   0   1   2   3   4
month1   5   6   7   8   9
month2  10  11  12  13  14
month3  15  16  17  18  19
  • 变百分数
    • 按列(即投影到列)求和
    • 按行(即投影到行)除法
>>> df.div(df.sum(axis=1), axis=0)
               a         b    c         d         e
month0  0.000000  0.100000  0.2  0.300000  0.400000
month1  0.142857  0.171429  0.2  0.228571  0.257143
month2  0.166667  0.183333  0.2  0.216667  0.233333
month3  0.176471  0.188235  0.2  0.211765  0.223529

猜你喜欢

转载自blog.csdn.net/a19990412/article/details/90745056