参考链接: Python数据分析与展示
参考链接: Pandas官网
参考链接: User Guide
参考链接: Getting started tutorials
基本统计分析函数:
适用于Series和DataFrame类型:
方法 | 说明 |
---|---|
.sum() | 计算数据的总和,按0轴计算,下同 |
.count() | 非NaN值的数量 |
.mean() .median() | 计算数据的算术平均值、算术中位数 |
.var() .std() | 计算数据的方差、标准差 |
.min() .max() | 计算数据的最小值、最大值 |
.describe() | 针对0轴(各列)的统计汇总 |
适用于Series类型:
方法 | 说明 |
---|---|
.argmin()和.argmax() | 计算数据最大值、最小值所在位置的索引位置(自动索引) |
.idxmin()和.idxmax() | 计算数据最大值、最小值所在位置的索引(自定义索引) |
实验演示1,在Series对象上的操作:
Microsoft Windows [版本 10.0.18363.1198]
(c) 2019 Microsoft Corporation。保留所有权利。
C:\Users\chenxuqi>python
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> import numpy as np
>>> a = pd.Series([9,8,7,6],index=['a','b','c','d'])
>>> a
a 9
b 8
c 7
d 6
dtype: int64
>>> a.describe()
count 4.000000
mean 7.500000
std 1.290994
min 6.000000
25% 6.750000
50% 7.500000
75% 8.250000
max 9.000000
dtype: float64
>>> type(a.describe())
<class 'pandas.core.series.Series'>
>>> type(a)
<class 'pandas.core.series.Series'>
>>> a.describe()['count']
4.0
>>> a.describe()['max']
9.0
>>>
>>>
实验演示2,在DataFrame对象上的操作:
>>>
>>> ##################################################
... # DataFrame类型上的操作
... b = pd.DataFrame(np.arange(20).reshape(4,5),index=['c','a','d','b'])
>>> b.describe()
0 1 2 3 4
count 4.000000 4.000000 4.000000 4.000000 4.000000
mean 7.500000 8.500000 9.500000 10.500000 11.500000
std 6.454972 6.454972 6.454972 6.454972 6.454972
min 0.000000 1.000000 2.000000 3.000000 4.000000
25% 3.750000 4.750000 5.750000 6.750000 7.750000
50% 7.500000 8.500000 9.500000 10.500000 11.500000
75% 11.250000 12.250000 13.250000 14.250000 15.250000
max 15.000000 16.000000 17.000000 18.000000 19.000000
>>> type(b.describe())
<class 'pandas.core.frame.DataFrame'>
>>> type(b.describe())
<class 'pandas.core.frame.DataFrame'>
>>> # b.describe().ix['max']
... # 这行代码在新版本中会报错
... # AttributeError: 'DataFrame' object has no attribute 'ix'
... # 使用如下代码: b.describe().loc['max']
... b.describe().loc['max']
0 15.0
1 16.0
2 17.0
3 18.0
4 19.0
Name: max, dtype: float64
>>> b.describe().loc['max']
0 15.0
1 16.0
2 17.0
3 18.0
4 19.0
Name: max, dtype: float64
>>> b.describe()[2]
count 4.000000
mean 9.500000
std 6.454972
min 2.000000
25% 5.750000
50% 9.500000
75% 13.250000
max 17.000000
Name: 2, dtype: float64
>>>
>>>
>>>
实验3,# 随机数下的统计:
>>>
>>>
>>> # 随机数下的统计
... np.random.seed(20200910)
>>> b = pd.DataFrame(np.random.randint(0,100,(4,5)),index=["c","a","d","b"])
>>> b
0 1 2 3 4
c 48 10 29 84 20
a 48 9 22 12 6
d 11 35 24 7 85
b 99 88 84 42 42
>>> b.describe()
0 1 2 3 4
count 4.000000 4.000000 4.000000 4.000000 4.00000
mean 51.500000 35.500000 39.750000 36.250000 38.25000
std 36.152455 37.009008 29.646529 35.387145 34.50966
min 11.000000 9.000000 22.000000 7.000000 6.00000
25% 38.750000 9.750000 23.500000 10.750000 16.50000
50% 48.000000 22.500000 26.500000 27.000000 31.00000
75% 60.750000 48.250000 42.750000 52.500000 52.75000
max 99.000000 88.000000 84.000000 84.000000 85.00000
>>> b.sum()
0 206
1 142
2 159
3 145
4 153
dtype: int64
>>> b.count()
0 4
1 4
2 4
3 4
4 4
dtype: int64
>>> b.mean()
0 51.50
1 35.50
2 39.75
3 36.25
4 38.25
dtype: float64
>>> b.median()
0 48.0
1 22.5
2 26.5
3 27.0
4 31.0
dtype: float64
>>> b.var()
0 1307.000000
1 1369.666667
2 878.916667
3 1252.250000
4 1190.916667
dtype: float64
>>> b.std()
0 36.152455
1 37.009008
2 29.646529
3 35.387145
4 34.509660
dtype: float64
>>> b.min()
0 11
1 9
2 22
3 7
4 6
dtype: int32
>>> b.max()
0 99
1 88
2 84
3 84
4 85
dtype: int32
>>> b1 = b[1]
>>> b1
c 10
a 9
d 35
b 88
Name: 1, dtype: int32
>>> b1.argmin()
1
>>> b1.idxmin()
'a'
>>> b1.argmax()
3
>>> b1.idxmax()
'b'
>>>
>>>