1. 主要的数值计算方法
1.1 .count()
→ count统计非Na值的数量
import pandas as pd
import numpy as np
df = pd.DataFrame({'key1':np.arange(10),
'key2':np.random.rand(10)*10})
print(df)
print(df.count())
–> 输出的结果为:
key1 key2
0 0 0.120548
1 1 9.706211
2 2 1.279955
3 3 4.436398
4 4 1.412675
5 5 8.526635
6 6 7.934625
7 7 4.552700
8 8 9.777576
9 9 0.705340
key1 10
key2 10
dtype: int64
1.2 .min()
→ min统计最小值, .max()
→ max统计最大值
可以针对全局数据,也可以针对列或者行中的数据
print(df.min(),df['key2'].max())
–> 输出的结果为:
key1 0.000000
key2 0.120548
dtype: float64
9.777575893716993
1.3 .quantile(q=0.75)
→ quantile统计分位数,参数q确定位置
可以直接通过df.describe()
查看
print(df.quantile(q=0.75))
–> 输出的结果为:
key1 6.750000
key2 8.378632
Name: 0.75, dtype: float64
1.4 .sum()
→ sum求和
print(df.sum())
–> 输出的结果为:
key1 45.000000
key2 48.452662
dtype: float64
1.5 .mean()
→ mean求平均值
print(df.mean())
–> 输出的结果为:
key1 4.500000
key2 4.845266
dtype: float64
1.6 .median()
→ median求算数中位数,50%分位数
print(df.median())
–> 输出的结果为:
key1 4.500000
key2 4.494549
dtype: float64
1.7 .std() /.var()
→ std,var分别求标准差,方差
print(df.std(),'\n',df.var())
–> 输出的结果为:
key1 3.027650
key2 3.876739
dtype: float64
key1 9.166667
key2 15.029103
dtype: float64
2. 基本参数
2.1 axis
参数:默认为0,以列来计算,axis=1
,以行来计算
np.nan
: 空值
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)
m1 = df.mean()
print(m1)
print(df['key2'].mean())
m2 = df.mean(axis=1)
print(m2)
–> 输出的结果为:(注意key3中的数值类型,被标记为object,而不是数值型,所以在计算均值的时候未纳入计算)
key1 key2 key3
a 4.0 1.0 1
b 5.0 2.0 2
c 3.0 NaN 3
d NaN 4.0 j
e 2.0 5.0 k
float64 float64 object
key1 3.5
key2 3.0
dtype: float64
3.0
a 2.5
b 3.5
c 3.0
d 4.0
e 3.5
dtype: float64
2.2 skipna
参数:是否忽略NaN,默认True,如False,有NaN的列统计结果仍未NaN
m3 = df.mean(skipna=False)
print(m3)
–> 输出的结果为:(注意这里将默认参数True改成了False)
key1 NaN
key2 NaN
dtype: float64
3. 统计基础(核心要点)
样本数据生成
df = pd.DataFrame({'key1':np.arange(10),
'key2':np.random.rand(10)*10})
print(df)
–> 输出的结果为:
key1 key2
0 0 6.623854
1 1 6.399347
2 2 0.491528
3 3 5.995801
4 4 9.560413
5 5 9.155879
6 6 0.856484
7 7 3.242136
8 8 0.941016
9 9 5.649808
3.1 .cumsum()
→ 累计和
df['key1_s'] = df['key1'].cumsum()
df['key2_s'] = df['key2'].cumsum()
print(df)
–> 输出的结果为:(这里以列进行举例,可以想下如何求按行累计和)
key1 key2 key1_s key2_s
0 0 6.623854 0 6.623854
1 1 6.399347 1 13.023202
2 2 0.491528 3 13.514729
3 3 5.995801 6 19.510530
4 4 9.560413 10 29.070943
5 5 9.155879 15 38.226822
6 6 0.856484 21 39.083306
7 7 3.242136 28 42.325442
8 8 0.941016 36 43.266458
9 9 5.649808 45 48.916266
3.2 .cumprod()
→ 累计积
df['key1_p'] = df['key1'].cumprod()
df['key2_p'] = df['key2'].cumprod()
print(df)
–> 输出的结果为:
key1 key2 key1_s key2_s key1_p key2_p
0 0 6.623854 0 6.623854 0 6.623854
1 1 6.399347 1 13.023202 0 42.388344
2 2 0.491528 3 13.514729 0 20.835046
3 3 5.995801 6 19.510530 0 124.922784
4 4 9.560413 10 29.070943 0 1194.313365
5 5 9.155879 15 38.226822 0 10934.988915
6 6 0.856484 21 39.083306 0 9365.642938
7 7 3.242136 28 42.325442 0 30364.688914
8 8 0.941016 36 43.266458 0 28573.648414
9 9 5.649808 45 48.916266 0 161435.635527
3.3 .cummax() / .cummin()
分别求累计最大值,累计最小值
print(df.cummax(),'\n',df.cummin())
–> 输出的结果为:(就是寻找累计中的最大、最小值)
key1 key2 key1_s key2_s key1_p key2_p
0 0.0 6.623854 0.0 6.623854 0.0 6.623854
1 1.0 6.623854 1.0 13.023202 0.0 42.388344
2 2.0 6.623854 3.0 13.514729 0.0 42.388344
3 3.0 6.623854 6.0 19.510530 0.0 124.922784
4 4.0 9.560413 10.0 29.070943 0.0 1194.313365
5 5.0 9.560413 15.0 38.226822 0.0 10934.988915
6 6.0 9.560413 21.0 39.083306 0.0 10934.988915
7 7.0 9.560413 28.0 42.325442 0.0 30364.688914
8 8.0 9.560413 36.0 43.266458 0.0 30364.688914
9 9.0 9.560413 45.0 48.916266 0.0 161435.635527
key1 key2 key1_s key2_s key1_p key2_p
0 0.0 6.623854 0.0 6.623854 0.0 6.623854
1 0.0 6.399347 0.0 6.623854 0.0 6.623854
2 0.0 0.491528 0.0 6.623854 0.0 6.623854
3 0.0 0.491528 0.0 6.623854 0.0 6.623854
4 0.0 0.491528 0.0 6.623854 0.0 6.623854
5 0.0 0.491528 0.0 6.623854 0.0 6.623854
6 0.0 0.491528 0.0 6.623854 0.0 6.623854
7 0.0 0.491528 0.0 6.623854 0.0 6.623854
8 0.0 0.491528 0.0 6.623854 0.0 6.623854
9 0.0 0.491528 0.0 6.623854 0.0 6.623854
3.4 .unique()
→ 唯一值
可以得到一个唯一值数组,然后通过pd.Series重新变成新的Series
s = pd.Series(list('asdvasdcfgg'))
sq = s.unique()
print(s)
print(sq,type(sq))
print(pd.Series(sq))
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']
★★★★★3.5 .value_counts()
→ 值计数
1) 得到一个新的Series,计算出不同值出现的频率
sort
参数:排序,默认为True
sc = s.value_counts(sort = False)
print(sc)
–> 输出的结果为:
s 2
d 2
v 1
c 1
a 2
g 2
f 1
dtype: int64
★★★★★3.6 .isin()
→ 成员资格
1) 数据生成及成员资格判断
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]))
–> 输出的结果为:
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
2) 选取目标元素(只需要在外层加上一层df[]
即可)
print(df[df.isin(['a','bc','10',8])])
–> 输出的结果为:
key1 key2
0 a NaN
1 NaN NaN
2 NaN NaN
3 NaN NaN
4 NaN 8.0
5 NaN NaN
6 a NaN
7 NaN NaN
8 NaN NaN
★★★ 3) .str.contains()
方法与 .isin()
方法的区别
这里会直接提取包含判断字符的数据,去除空值
print(df[df['key1'].str.contains('a')])
print(df[df['key1'].isin(['a'])])
–> 输出的结果为:(这里的输出结果是一致的,取得都是key1中的数据,均为字符类型)
key1 key2
0 a 4
6 a 10
key1 key2
0 a 4
6 a 10
print(df[df['key2'].isin([4])])
print(df[df['key2'].str.contains(4)])
–> 输出的结果为:(这里就可以看出区别 ,当数据类型是数值时候,用.str.contains方法行不通)
key1 key2
0 a 4
第二种用法报错:AttributeError: Can only use .str accessor with string values!