pandas之数值计算与统计

数值计算与统计

 

对于DataFrame求和、最大、最小、平均等统计方法来说,默认是按列进行统计,即axis = 0,如果添加参数axis = 1则会按照行进行统计。

如果存在空值,在统计时默认会忽略空值,如果添加参数skipna = False,统计时不会忽略空值。

 

count()  非NaN的元素个数

sum()  和

mean()  平均值

median()  中位数

max()  最大值

min()  最小值

std()  标准差

var()  方差

skew(),样本的偏度

kurt(),样本的峰度

 

分位数

quantile(q=0.5,axis=0),统计分位数,q确定位置,默认为0.5,axix=0默认按行统计,1按列统计  【适用于Seris和DataFrame】

计算逻辑:r = i + ( j - i ) * f

①将行或者列按数值大小升序排序,并计算位置pos = 1+(n-1)*q,其中n为行或列的长度,q为定义的参数

②根据pos确定 i 和 j,例如计算得pos=3.2,则i为第3个数,j为第4个数,f为pos的小数部分

dic = {'one':[1,3,2,5,4],'two':[2,4,3,6,5],'three':[3,7,5,6,4]}
df = pd.DataFrame(dic,index=list('abcde'))
print(df)
print(df.quantile(0.1))
print(df.quantile([0.5,0.7]))
#    one  two  three
# a    1    2      3
# b    3    4      7
# c    2    3      5
# d    5    6      6
# e    4    5      4
# one      1.4
# two      2.4
# three    3.4
# Name: 0.1, dtype: float64
#      one  two  three
# 0.5  3.0  4.0    5.0
# 0.7  3.8  4.8    5.8
分位数

以上以quantile(q=0.7)为例讲解,按照列进行统计,每列的长度为5

pos = 1 + ( 5 - 1 ) * 0.7 = 3.8,因此i为每列的第3位数,j为每列的第4位数,且f为3.8的小数部分即0.8

result_one = 3 + ( 4 - 3 ) * 0.8 = 3.8

result_two = 4 + ( 5 - 4 ) * 0.8 = 4.8

reslut_three = 5+ ( 6 - 5 ) * 0.8 = 5.8

 

上四分位和下四分位确定位置pos = (n-1)/4和pos=3* (n-1)/4

 

累计值

cumsum() 累计和、cumprod() 累计积、cummax()累计最大值、cummin()累计最小值【适用于Seris和DataFrame】

dic = {'one':[1,3,2,5,4],'two':[2,4,3,6,5]}
df = pd.DataFrame(dic,index=list('abcde'))
df['one_cumsum'] = df['one'].cumsum()  #相当于增加一列
df['one_cumprod'] = df['one'].cumprod()
df['two_cummax'] = df['two'].cummax()
df['two_cummin'] = df['two'].cummin()
print(df)
#    one  two  one_cumsum  one_cumprod  two_cummax  two_cummin
# a    1    2           1            1           2           2
# b    3    4           4            3           4           2
# c    2    3           6            6           4           2
# d    5    6          11           30           6           2
# e    4    5          15          120           6           2
累计和、累计积、累计最大值、累计最小值

 

唯一值

对序列进行唯一值unique()之后生成的是一维数组 【适用于Seris】

s = pd.Series(list('abaefb'))
print(s)
sq = s.unique()
print(sq,type(sq))
sq_s = pd.Series(sq)
# 0    a
# 1    b
# 2    a
# 3    e
# 4    f
# 5    b
# dtype: object
# ['a' 'b' 'e' 'f'] <class 'numpy.ndarray'>
Seris的unique()

 

值计数

value_counts(),统计Seris中相同的值出现的次数,生成一个新的Seris,新Seris的index为原来的值,值为出现的次数 【适用于Seris】

s = pd.Series(list('abaefb'))
s_count = s.value_counts()
print(s)
print(s_count,type(s_count))
# 0    a
# 1    b
# 2    a
# 3    e
# 4    f
# 5    b
# dtype: object
# a    2
# b    2
# f    1
# e    1
# dtype: int64 <class 'pandas.core.series.Series'>
Seris的值计数

 

成员判断

isin([ ]),成员要使用中括号括起来,判断每个元素是否在中括号的元素中,生成的结果为布尔型的Seris或DataFrame   【适用于Seris和DataFrame】

# s = pd.Series(list('abced'))
# df = pd.DataFrame(np.arange(6).reshape(2,3),columns=['a','b','c'])
# print(s.isin(['a','b']))
# print(df.isin([1,2]))
# 0     True
# 1     True
# 2    False
# 3    False
# 4    False
# dtype: bool
#        a      b      c
# 0  False   True   True
# 1  False  False  False
成员判断

 

转载于:https://www.cnblogs.com/Forever77/p/11259146.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值