Pandas提供了大量與數據探索相關的函數。這些統計特徵函數能反映出數據的總體分佈,主要做爲Pandas的對象DataFrame或Series的方法出現。
sum():計算數據樣本的總和(按列計算) html
mean():計算數據樣本的算術平均數 python
var():計算數據樣本的方差 app
std():計算數據樣本的標準差 函數
corr():計算數據樣本的Spearman(Pearson)相關係數矩陣 spa
cov():計算數據樣本的協方差矩陣 .net
skew():樣本值的偏度(三階矩) code
kurt():樣本值的峯度(四階矩)
describe():給出樣本的基本描述(基本統計量如均值、標準差等)orm
import pandas as pd import sys
print('Python version ' + sys.version) print('Pandas version ' + pd.__version__)
# 建立一個以日期爲索引的數據幀
States = ['NY', 'NY', 'NY', 'NY', 'FL', 'FL', 'GA', 'GA', 'FL', 'FL']
data = [1.0, 2, 3, 4, 5, 6, 7, 8, 9, 10]
idx = pd.date_range('1/1/2012', periods=10, freq='MS')
df1 = pd.DataFrame(data, index=idx, columns=['Revenue'])
df1['State'] = States
#建立第二個數據幀
data2 = [10.0, 10.0, 9, 9, 8, 8, 7, 7, 6, 6]
idx2 = pd.date_range('1/1/2013', periods=10, freq='MS')
df2 = pd.DataFrame(data2, index=idx2, columns=['Revenue']) df2['State'] = States
請參考pandas中時間序列——date_range函數
# 合併數據幀
df = pd.concat([df1,df2]) df
注意:平均誤差和標準誤差僅適用於高斯分佈。htm
In [5]:對象
# 方法 1
# 建立df的一個拷貝 newdf = df.copy() newdf['x-Mean'] = abs(newdf['Revenue'] - newdf['Revenue'].mean()) newdf['1.96*std'] = 1.96*newdf['Revenue'].std() newdf['Outlier'] = abs(newdf['Revenue'] - newdf['Revenue'].mean()) > 1.96*newdf['Revenue'].std() newdf
Out[5]:
# 方法 2
# 按項分組 #建立df的一個拷貝
newdf = df.copy() State = newdf.groupby('State') newdf['Outlier'] = State.transform( lambda x: abs(x-x.mean()) > 1.96*x.std() ) newdf['x-Mean'] = State.transform( lambda x: abs(x-x.mean()) ) newdf['1.96*std'] = State.transform( lambda x: 1.96*x.std() ) newdf
# Method 2
# Group by multiple items # make a copy of original df newdf = df.copy() StateMonth = newdf.groupby(['State', lambda x: x.month]) newdf['Outlier'] = StateMonth.transform( lambda x: abs(x-x.mean()) > 1.96*x.std() ) newdf['x-Mean'] = StateMonth.transform( lambda x: abs(x-x.mean()) ) newdf['1.96*std'] = StateMonth.transform( lambda x: 1.96*x.std() ) newdf
# Method 3
# Group by item # make a copy of original df newdf = df.copy() State = newdf.groupby('State') def s(group): group['x-Mean'] = abs(group['Revenue'] - group['Revenue'].mean()) group['1.96*std'] = 1.96*group['Revenue'].std() group['Outlier'] = abs(group['Revenue'] - group['Revenue'].mean()) > 1.96*group['Revenue'].std() return group Newdf2 = State.apply(s) Newdf2
# Method 3
# Group by multiple items # make a copy of original df newdf = df.copy() StateMonth = newdf.groupby(['State', lambda x: x.month]) def s(group): group['x-Mean'] = abs(group['Revenue'] - group['Revenue'].mean()) group['1.96*std'] = 1.96*group['Revenue'].std() group['Outlier'] = abs(group['Revenue'] - group['Revenue'].mean()) > 1.96*group['Revenue'].std() return group Newdf2 = StateMonth.apply(s) Newdf2
假設一個非高斯分佈(若是你繪製它,它看起來不像正態分佈)
# make a copy of original df
newdf = df.copy() State = newdf.groupby('State') newdf['Lower'] = State['Revenue'].transform( lambda x: x.quantile(q=.25) - (1.5*(x.quantile(q=.75)-x.quantile(q=.25))) ) newdf['Upper'] = State['Revenue'].transform( lambda x: x.quantile(q=.75) + (1.5*(x.quantile(q=.75)-x.quantile(q=.25))) ) newdf['Outlier'] = (newdf['Revenue'] < newdf['Lower']) | (newdf['Revenue'] > newdf['Upper']) newdf
This tutorial wasrewrited by CDS