我有一个包含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
答案 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
和%
计算
df.div(df.sum())
然后使用上面的代码
pd.concat([df,
pd.DataFrame(df.div(df.sum()).values * 100,
columns=df.columns.values + '%')],
axis=1)