Pandas计算DataFrame的所有列的行总值、均值、最大值、最小值、极差、方差

说明

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

输出:

Pandas计算DataFrame的所有列的行总值、均值、最大值、最小值、极差、方差_第1张图片

计算相关指标

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

输出:
Pandas计算DataFrame的所有列的行总值、均值、最大值、最小值、极差、方差_第2张图片
如果不加
df = df.astype(int)
会输出小数,加上之后取整了

你可能感兴趣的:(Pandas和Numpy,pandas,python)