【课程2.14】 数值计算和统计基础
常用数学、统计方法
1.基本参数:axis、skipna
import numpy as np
import pandas as pd
df = pd.DataFrame({'key1':[4,5,3,np.nan,2],
'key2':[1,2,np.nan,4,5],
'key3':[1,2,3,'j','k']},
index = ['a','b','c','d','e'])
print(df)
print(df['key1'].dtype,df['key2'].dtype,df['key3'].dtype)
print('-----')
m1 = df.mean()
print(m1,type(m1))
print('单独统计一列:',df['key2'].mean())
print('-----')
# np.nan :空值
# .mean()计算均值
# 只统计数字列
# 可以通过索引单独统计一列
m2 = df.mean(axis=1)
print(m2)
print('-----')
# axis参数:默认为0,以列来计算,axis=1,以行来计算,这里就按照行来汇总了
m3 = df.mean(skipna=False)
print(m3)
print('-----')
# skipna参数:是否忽略NaN,默认True,如False,有NaN的列统计结果仍未NaN
----------------------------------------------------------------------
# 基本参数:axis、skipna
import numpy as np
import pandas as pd
df = pd.DataFrame({'key1':[4,5,3,np.nan,2],
'key2':[1,2,np.nan,4,5],
'key3':[1,2,3,'j','k']},
index = ['a','b','c','d','e'])
print(df)
print(df['key1'].dtype,df['key2'].dtype,df['key3'].dtype)
print('-----')
m1 = df.mean()
print(m1,type(m1))
print('单独统计一列:',df['key2'].mean())
print('-----')
# np.nan :空值
# .mean()计算均值
# 只统计数字列
# 可以通过索引单独统计一列
m2 = df.mean(axis=1)
print(m2)
print('-----')
# axis参数:默认为0,以列来计算,axis=1,以行来计算,这里就按照行来汇总了
m3 = df.mean(skipna=False)
print(m3)
print('-----')
# skipna参数:是否忽略NaN,默认True,如False,有NaN的列统计结果仍未NaN
----------------------------------------------------------------------
2.主要数学计算方法,可用于Series和DataFrame(1)
df = pd.DataFrame({'key1':np.arange(10),
'key2':np.random.rand(10)*10})
print(df)
print('-----')
print(df.count(),'→ count统计非Na值的数量\n')
print(df.min(),'→ min统计最小值\n',df['key2'].max(),'→ max统计最大值\n')
print(df.quantile(q=0.75),'→ quantile统计分位数,参数q确定位置\n')
print(df.sum(),'→ sum求和\n')
print(df.mean(),'→ mean求平均值\n')
print(df.median(),'→ median求算数中位数,50%分位数\n')
print(df.std(),'\n',df.var(),'→ std,var分别求标准差,方差\n')
print(df.skew(),'→ skew样本的偏度\n')
print(df.kurt(),'→ kurt样本的峰度\n')
----------------------------------------------------------------------
key1 key2
0 0 4.667989
1 1 4.336625
2 2 0.746852
3 3 9.670919
4 4 8.732045
5 5 0.013751
6 6 8.963752
7 7 0.279303
8 8 8.586821
9 9 8.899657
-----
key1 10
key2 10
dtype: int64 → count统计非Na值的数量
key1 0.000000
key2 0.013751
dtype: float64 → min统计最小值
9.67091932107 → max统计最大值
key1 6.750000
key2 8.857754
dtype: float64 → quantile统计分位数,参数q确定位置
key1 45.000000
key2 54.897714
dtype: float64 → sum求和
key1 4.500000
key2 5.489771
dtype: float64 → mean求平均值
key1 4.500000
key2 6.627405
dtype: float64 → median求算数中位数,50%分位数
key1 3.027650
key2 3.984945
dtype: float64
key1 9.166667
key2 15.879783
dtype: float64 → std,var分别求标准差,方差
key1 0.000000
key2 -0.430166
dtype: float64 → skew样本的偏度
key1 -1.200000
key2 -1.800296
dtype: float64 → kurt样本的峰度
3.主要数学计算方法,可用于Series和DataFrame(2)
df['key1_s'] = df['key1'].cumsum()
df['key2_s'] = df['key2'].cumsum()
print(df,'→ cumsum样本的累计和\n')
df['key1_p'] = df['key1'].cumprod()
df['key2_p'] = df['key2'].cumprod()
print(df,'→ cumprod样本的累计积\n')
print(df.cummax(),'\n',df.cummin(),'→ cummax,cummin分别求累计最大值,累计最小值\n')
# 会填充key1,和key2的值
----------------------------------------------------------------------
key1 key2 key1_s key2_s
0 0 4.667989 0 4.667989
1 1 4.336625 1 9.004614
2 2 0.746852 3 9.751466
3 3 9.670919 6 19.422386
4 4 8.732045 10 28.154431
5 5 0.013751 15 28.168182
6 6 8.963752 21 37.131934
7 7 0.279303 28 37.411236
8 8 8.586821 36 45.998057
9 9 8.899657 45 54.897714 → cumsum样本的累计和
key1 key2 key1_s key2_s key1_p key2_p
0 0 4.667989 0 4.667989 0 4.667989
1 1 4.336625 1 9.004614 0 20.243318
2 2 0.746852 3 9.751466 0 15.118767
3 3 9.670919 6 19.422386 0 146.212377
4 4 8.732045 10 28.154431 0 1276.733069
5 5 0.013751 15 28.168182 0 17.556729
6 6 8.963752 21 37.131934 0 157.374157
7 7 0.279303 28 37.411236 0 43.955024
8 8 8.586821 36 45.998057 0 377.433921
9 9 8.899657 45 54.897714 0 3359.032396 → cumprod样本的累计积
key1 key2 key1_s key2_s key1_p key2_p
0 0.0 4.667989 0.0 4.667989 0.0 4.667989
1 1.0 4.667989 1.0 9.004614 0.0 20.243318
2 2.0 4.667989 3.0 9.751466 0.0 20.243318
3 3.0 9.670919 6.0 19.422386 0.0 146.212377
4 4.0 9.670919 10.0 28.154431 0.0 1276.733069
5 5.0 9.670919 15.0 28.168182 0.0 1276.733069
6 6.0 9.670919 21.0 37.131934 0.0 1276.733069
7 7.0 9.670919 28.0 37.411236 0.0 1276.733069
8 8.0 9.670919 36.0 45.998057 0.0 1276.733069
9 9.0 9.670919 45.0 54.897714 0.0 3359.032396
key1 key2 key1_s key2_s key1_p key2_p
0 0.0 4.667989 0.0 4.667989 0.0 4.667989
1 0.0 4.336625 0.0 4.667989 0.0 4.667989
2 0.0 0.746852 0.0 4.667989 0.0 4.667989
3 0.0 0.746852 0.0 4.667989 0.0 4.667989
4 0.0 0.746852 0.0 4.667989 0.0 4.667989
5 0.0 0.013751 0.0 4.667989 0.0 4.667989
6 0.0 0.013751 0.0 4.667989 0.0 4.667989
7 0.0 0.013751 0.0 4.667989 0.0 4.667989
8 0.0 0.013751 0.0 4.667989 0.0 4.667989
9 0.0 0.013751 0.0 4.667989 0.0 4.667989 → cummax,cummin分别求累计最大值,累计最小值
4.唯一值:.unique()
s = pd.Series(list('asdvasdcfgg'))
sq = s.unique()
print(s)
print(sq,type(sq))
print(pd.Series(sq))
# 得到一个唯一值数组
# 通过pd.Series重新变成新的Series
sq.sort()
print(sq)
# 重新排序
----------------------------------------------------------------------
0 a
1 s
2 d
3 v
4 a
5 s
6 d
7 c
8 f
9 g
10 g
dtype: object
['a' 's' 'd' 'v' 'c' 'f' 'g'] <class 'numpy.ndarray'>
0 a
1 s
2 d
3 v
4 c
5 f
6 g
dtype: object
['a' 'c' 'd' 'f' 'g' 's' 'v']
5.值计数:.value_counts()
sc = s.value_counts(sort = False) # 也可以这样写:pd.value_counts(sc, sort = False)
print(sc)
# 得到一个新的Series,计算出不同值出现的频率
# sort参数:排序,默认为True
----------------------------------------------------------------------
s 2
d 2
v 1
c 1
a 2
g 2
f 1
dtype: int64
6.成员资格:.isin()
s = pd.Series(np.arange(10,15))
df = pd.DataFrame({'key1':list('asdcbvasd'),
'key2':np.arange(4,13)})
print(s)
print(df)
print('-----')
print(s.isin([5,14]))
print(df.isin(['a','bc','10',8]))
# 用[]表示
# 得到一个布尔值的Series或者Dataframe
----------------------------------------------------------------------
0 10
1 11
2 12
3 13
4 14
dtype: int32
key1 key2
0 a 4
1 s 5
2 d 6
3 c 7
4 b 8
5 v 9
6 a 10
7 s 11
8 d 12
-----
0 False
1 False
2 False
3 False
4 True
dtype: bool
key1 key2
0 True False
1 False False
2 False False
3 False False
4 False True
5 False False
6 True False
7 False False
8 False False