Python 计算数据帧中跨行的百分比值

Python 计算数据帧中跨行的百分比值,python,pandas,dataframe,Python,Pandas,Dataframe,我用各种能源建立了一个数据框架,如何计算每一列对全年能源贡献的比例 year Biomass Energy Production Coal Production Crude Oil Production 1949 1.549262 11.973882 10.683252 1950 1.562307 14.060135 11.4467

我用各种能源建立了一个数据框架,如何计算每一列对全年能源贡献的比例

year    Biomass Energy Production  Coal Production  Crude Oil Production
1949                     1.549262        11.973882             10.683252
1950                     1.562307        14.060135             11.446729
1951                     1.534669        14.419325             13.036724
1952                     1.474369        12.734313             13.281049
1953                     1.418601        12.277746             13.671076
1954                     1.394327        10.542448             13.426930
1955                     1.424143        12.369608             14.409682
1956                     1.415871        13.306334             15.180241

很快,我就是这么做的:

import pandas as pd

df = pd.read_csv('energy.csv')
col_list=list(df)
col_list.remove('year')
df['total'] = df[col_list].sum(axis=1)
df1 = df.drop(['year'], axis=1)
percent = df1.div(df1.total, axis='index') * 100

>>> percent
   Biomass.Energy.Production  Coal.Production  Crude.Oil.Production  total
0                   6.400218        49.465778             44.134005    100
1                   5.771536        51.941506             42.286958    100
2                   5.293656        49.737730             44.968614    100
3                   5.363345        46.323891             48.312765    100
4                   5.183539        44.862631             49.953830    100
5                   5.497332        41.565095             52.937574    100
6                   5.049538        43.858519             51.091943    100
7                   4.734967        44.499149             50.765884    100
------------- 编辑:

编辑2:

df = pd.read_csv('energy.csv')
df = df.set_index(['year'])
percent = df.div(df.sum(1)/100,0)
df = df.reset_index('year')

很快,我就是这么做的:

import pandas as pd

df = pd.read_csv('energy.csv')
col_list=list(df)
col_list.remove('year')
df['total'] = df[col_list].sum(axis=1)
df1 = df.drop(['year'], axis=1)
percent = df1.div(df1.total, axis='index') * 100

>>> percent
   Biomass.Energy.Production  Coal.Production  Crude.Oil.Production  total
0                   6.400218        49.465778             44.134005    100
1                   5.771536        51.941506             42.286958    100
2                   5.293656        49.737730             44.968614    100
3                   5.363345        46.323891             48.312765    100
4                   5.183539        44.862631             49.953830    100
5                   5.497332        41.565095             52.937574    100
6                   5.049538        43.858519             51.091943    100
7                   4.734967        44.499149             50.765884    100
------------- 编辑:

编辑2:

df = pd.read_csv('energy.csv')
df = df.set_index(['year'])
percent = df.div(df.sum(1)/100,0)
df = df.reset_index('year')

好:)我很快会再看一遍。。。可以肯定的是,有一种更有效的方法,可以使用
set\u index
而不是删除“year”列。这将简化您的代码,如果您希望索引'year'成为一列,只需使用
reset\u index('year')
。这样,代码将是:
df.div(df.sum(1)/100,axis=0)
.Good:)我很快会再看一遍。。。可以肯定的是,有一种更有效的方法,可以使用
set\u index
而不是删除“year”列。这将简化您的代码,如果您希望索引'year'成为一列,只需使用
reset\u index('year')
。这样,代码将是:
df.div(df.sum(1)/100,axis=0)