美文网首页
Series及DataFrame数值计算和统计基础函数应用总结

Series及DataFrame数值计算和统计基础函数应用总结

作者: 越大大雨天 | 来源:发表于2019-03-30 09:56 被阅读0次
  • pandas数值统计计算基础函数,count()、min()、quantile()、sum()、mean()、median()、std()、skew()、kurt():
# 主要数学计算方法,可用于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样本的峰度
  • cumsum()、cumprod()数学累加和、累加积计算方法:
# 主要数学计算方法,可用于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分别求累计最大值,累计最小值
  • unique()唯一值使用:
# 唯一值:.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']
  • 按频率计数:value_counts()
# 值计数:.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
  • isin()判断元素是否在指定范围
# 成员资格:.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

相关文章

网友评论

      本文标题:Series及DataFrame数值计算和统计基础函数应用总结

      本文链接:https://www.haomeiwen.com/subject/ivtjbqtx.html