有不少方法用來集體計算DataFrame
的描述性統計信息和其餘相關操做。 其中大多數是sum()
,mean()
等聚合函數。 通常來講,這些方法採用軸參數,就像ndarray.{sum,std,...}
,但軸能夠經過名稱或整數來指定:shell
下面建立一個數據幀(DataFrame),並使用此對象進行演示本章中全部操做。數組
import pandas as pd d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack', 'Lee','David','Gasper','Betina','Andres']), 'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])} df = pd.DataFrame(d) print(df)
輸出結果:數據結構
Age Name Rating 0 25 Tom 4.23 1 26 James 3.24 2 25 Ricky 3.98 3 23 Vin 2.56 4 30 Steve 3.20 5 29 Minsu 4.60 6 23 Jack 3.80 7 34 Lee 3.78 8 40 David 2.98 9 30 Gasper 4.80 10 51 Betina 4.10 11 46 Andres 3.65
返回所請求軸的值的總和。 默認狀況下,軸爲列名(axis=0
)。函數
import pandas as pd d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack', 'Lee','David','Gasper','Betina','Andres']), 'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])} df = pd.DataFrame(d) print(df.sum())
輸出結果:spa
Age 382 Name TomJamesRickyVinSteveMinsuJackLeeDavidGasperBe... Rating 44.92 dtype: object
示例axis=1code
import pandas as pd d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack', 'Lee','David','Gasper','Betina','Andres']), 'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])} df = pd.DataFrame(d) print(df.sum(1))
輸出結果:對象
0 29.23 1 29.24 2 28.98 3 25.56 4 33.20 5 33.60 6 26.80 7 37.78 8 42.98 9 34.80 10 55.10 11 49.65 dtype: float64
mean()
返回平均值blog
import pandas as pd d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack', 'Lee','David','Gasper','Betina','Andres']), 'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])} df = pd.DataFrame(d) print(df.mean())
輸出結果:字符串
Age 31.833333 Rating 3.743333 dtype: float64
返回標準差。pandas
import pandas as pd d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack', 'Lee','David','Gasper','Betina','Andres']), 'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])} df = pd.DataFrame(d) print(df.std())
輸出結果:
Age 9.232682 Rating 0.661628 dtype: float64
下面來了解Python Pandas中描述性統計信息的函數,下表列出了重要函數
編號 | 函數 | 描述 |
---|---|---|
1 | count() |
非空觀測數量 |
2 | sum() |
全部值之和 |
3 | mean() |
全部值的平均值 |
4 | median() |
全部值的中位數 |
5 | mode() |
值的模值 |
6 | std() |
值的標準誤差 |
7 | min() |
全部值中的最小值 |
8 | max() |
全部值中的最大值 |
9 | abs() |
絕對值 |
10 | prod() |
數組元素的乘積 |
11 | cumsum() |
累計總和 |
12 | cumprod() |
累計乘積 |
注 - 因爲DataFrame是異構數據結構。通用操做不適用於全部函數。
sum()
,cumsum()
函數能與數字和字符(或)字符串數據元素一塊兒工做,不會產生任何錯誤。字符聚合歷來都比較少被使用,雖然這些函數不會引起任何異常。abs()
,cumprod()
這樣的函數會拋出異常。describe()
函數是用來計算有關DataFrame列的統計信息的摘要。
1. 描述數字系列
import pandas as pd d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack', 'Lee','David','Gasper','Betina','Andres']), 'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])} df = pd.DataFrame(d) print(df.describe())
輸出結果:
Age Rating count 12.000000 12.000000 mean 31.833333 3.743333 std 9.232682 0.661628 min 23.000000 2.560000 25% 25.000000 3.230000 50% 29.500000 3.790000 75% 35.500000 4.132500 max 51.000000 4.800000
2. 描述一個分類系列
import pandas as pd s = pd.Series(['a', 'a', 'b', 'c']) print(s.describe())
輸出結果:
count 4
unique 3
top a
freq 2
dtype: object
其結果包括count,unique,top,和freq。時間數據還包括first和last項目。
count:同上
unique:表示有多少種不一樣的值
top:數據中出現次數最高的值
freq:出現次數最高的那個值(top)的出現頻率
3. 描述時間戳系列
import pandas as pd import numpy as np s = pd.Series([np.datetime64("2000-01-01"), np.datetime64("2010-01-01"), np.datetime64("2010-01-01") ]) print(s.describe())
輸出結果:
count 3
unique 2
top 2010-01-01 00:00:00
freq 2
first 2000-01-01 00:00:00
last 2010-01-01 00:00:00
dtype: object
使用include和exclude參數來限制DataFrame中哪些列被分析輸出
object
- 彙總字符串列number
- 彙總數字列all
- 將全部列彙總在一塊兒(不該將其做爲列表值傳遞)(1)若是include ='all'做爲選項提供,全部列,而無論數據類型如何。
import pandas as pd d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack','Lee','David','Gasper','Betina','Andres']), 'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])} df = pd.DataFrame(d) print(df.describe(include='all'))
輸出結果:
Name Age Rating
count 12 12.000000 12.000000
unique 12 NaN NaN
top Steve NaN NaN
freq 1 NaN NaN
mean NaN 31.833333 3.743333
std NaN 9.232682 0.661628
min NaN 23.000000 2.560000
25% NaN 25.000000 3.230000
50% NaN 29.500000 3.790000
75% NaN 35.500000 4.132500
max NaN 51.000000 4.800000
(2)在DataFrame描述中只包含字符串列
import pandas as pd import numpy as np d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack','Lee','David','Gasper','Betina','Andres']), 'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])} df = pd.DataFrame(d) print(df.describe(include=[np.object]))
(3)在DataFrame描述中僅包含數字列
import pandas as pd import numpy as np d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack','Lee','David','Gasper','Betina','Andres']), 'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])} df = pd.DataFrame(d) print(df.describe(include=[np.number]))
輸出結果:
Age Rating
count 12.000000 12.000000
mean 31.833333 3.743333
std 9.232682 0.661628
min 23.000000 2.560000
25% 25.000000 3.230000
50% 29.500000 3.790000
75% 35.500000 4.132500
max 51.000000 4.800000
從DataFrame描述中排除對象列。
import pandas as pd import numpy as np d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack','Lee','David','Gasper','Betina','Andres']), 'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])} df = pd.DataFrame(d) print(df.describe(exclude=[np.object]))
輸出結果:
Age Rating
count 12.000000 12.000000
mean 31.833333 3.743333
std 9.232682 0.661628
min 23.000000 2.560000
25% 25.000000 3.230000
50% 29.500000 3.790000
75% 35.500000 4.132500
max 51.000000 4.800000
import pandas as pd d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack', 'Lee','David','Gasper','Betina','Andres']), 'Age':pd.Series([25,26,25,23,30,29,23,34,40,30,51,46]), 'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8,3.78,2.98,4.80,4.10,3.65])} df = pd.DataFrame(d) print(df.describe(include=['object']))
輸出結果:
Name count 12 unique 12 top Ricky freq 1