CHAPTER 5
Getting Started with pandas
文章目录
5.3 汇总和计算描述性统计(Summarizing and Computing Descriptive Statistics)
pandas
有很多数学和统计方法。大部分可以归类为降维或汇总统计,这些方法是用来从series中提取单个值(比如sum
或mean
)。还有一些方法来处理缺失值:
>>>import pandas as pd
>>>import numpy as np
>>>df = pd.DataFrame([[1.4, np.nan], [7.1, -4.5],
[np.nan, np.nan], [0.75, -1.3]],
index=['a', 'b', 'c', 'd'],
columns=['one', 'two'])
>>>df
one two
a 1.40 NaN
b 7.10 -4.5
c NaN NaN
d 0.75 -1.3
使用sum
的话,会返回一个series:
>>>df.sum() #默认对列求和
one 9.25
two -5.80
dtype: float64
使用axis='columns'
or axis=1
,计算列之间的和:
>>>df.sum(axis='columns')
a 1.40
b 2.60
c 0.00
d -0.55
dtype: float64
计算的时候,除非整个切片全是NA,否则NA(即缺失值)会被除外。我们可以用参数skipna
来跳过计算NA:
>>>df.mean(axis='columns', skipna=False)
a NaN
b 1.300
c NaN
d -0.275
dtype: float64
reduction methods
方法 | 描述 |
---|---|
axis | DataFrame的行用 0,列用 1 |
skipna | 排除缺失值,默认值为True |
level | 如果轴是分层索引的(MultiIndex),则按级别减少分组 |
举几个栗子,idxmin
和idxmax
,能返回间接的统计值,比如index value:
>>>df.idxmax()
one b
two d
dtype: object
计算累加值:
>>>df.cumsum()
one two
a 1.40 NaN
b 8.50 -4.5
c NaN NaN
d 9.25 -5.8
describe
能一下子产生多维汇总数据,我感觉就类似R语言
中的summary()
函数。
对于数值型数据:
>>>df.describe()
one two
count 3.000000 2.000000
mean 3.083333 -2.900000
std 3.493685 2.262742
min 0.750000 -4.500000
25% 1.075000 -3.700000
50% 1.400000 -2.900000
75% 4.250000 -2.100000
max 7.100000 -1.300000
对于非数值型数据: