Pandas计算DataFrame的所有列的行总值、均值、最大值、最小值、极差、方差
import pandas as pd
data = {"ID":[202001, 202002, 202003, 202004, 202005, 202006, 202007, 202008, 202009, 202010],
"Chinese":[98, 67, 84, 88, 78, 90, 93, 75, 82, 87],
"Math":[92, 80, 73, 76, 88, 78, 90, 82, 77, 69],
"English":[88, 79, 90, 73, 79, 83, 81, 91, 71, 78]
}
df = pd.DataFrame(data)
df
输出:
df['SumScore'] = df[['Chinese','Math','English']].sum(axis=1)
df['MeanScore'] = df[['Chinese','Math','English']].mean(axis=1)
df['MaxScore'] = df[['Chinese','Math','English']].max(axis=1)
df['MinScore'] = df[['Chinese','Math','English']].min(axis=1)
df['PtpScore'] = df['MaxScore']-df['MinScore']
df['VarScore'] = df[['Chinese','Math','English']].var(axis=1)
df = df.astype(int)
df