如何从熊猫表中获取每个值的百分比?

时间:2016-09-27 21:07:55

标签: python-3.x pandas

我有一个包含5列的表格:AWA,REM,S1,S2和SWS。我计算了每列和每行的总和。还有这些价值的百分比。但为了获得百分比,我重复了同样的5次。 如果有超过5列,有没有办法改进它?

这是我的代码,我还附上了一张图片。

    import pandas as pd

    df = pd.DataFrame(TABLA, columns=('AWA', 'REM', 'S1', 'S2', 'SWS'))
    df.index='s' + (df.index+1).astype(str) 
    df['xSubject'] = df.sum(axis=1) #sums each colummn

    ######Here starts the repetition:
    df['AWA%'] = df['AWA']/df['AWA'].sum()*100
    df['REM%'] = df['REM']/df['REM'].sum()*100
    df['S1%'] = df['S1']/df['S1'].sum()*100
    df['S2%'] = df['S2']/df['S2'].sum()*100
    df['SWS%'] = df['SWS']/df['SWS'].sum()*100
    df['xSubject%'] = df['xSubject']/df['xSubject'].sum()*100
    ######Here ends the repetition:

    df.loc['xStage'] = df.sum() #sums each row

    df

enter image description here

1 个答案:

答案 0 :(得分:2)

pd.concat与重建

一起使用
pd.concat([df,
           pd.DataFrame(df.div(df.sum()).values * 100,
                        columns=df.columns.values + '%')],
          axis=1)

考虑pd.DataFrame df

df = pd.DataFrame(np.random.rand(10, 5),
                  columns=('AWA', 'REM', 'S1', 'S2', 'SWS'))

df

enter image description here

%计算

df.div(df.sum())

enter image description here

然后使用上面的代码

pd.concat([df,
           pd.DataFrame(df.div(df.sum()).values * 100,
                        columns=df.columns.values + '%')],
          axis=1)

enter image description here