整理20个Pandas统计函数(上)

简介: 整理20个Pandas统计函数

模拟数据


为了解释每个函数的使用,模拟了一份带有空值的数据:


importpandasaspd importnumpyasnp importmatplotlib.pyplotasplt importseabornassns df=pd.DataFrame({ "sex":["male","male","female","female","male"], "age":[22,24,25,26,24], "chinese":[100,120,110,100,90], "math":[90,np.nan,100,80,120],#存在空值 "english":[90,130,90,80,100]}) df


描述统计信息describe


descirbe方法只能针对序列或数据框,一维数组是没有这个方法的;同时默认只能针对数值型的数据进行统计:


DataFrame.describe(percentiles=None,include=None,exclude=None)


  • percentiles:可选折的百分数,列表形式;数值在0-1之间,默认是[.25,.5,.75]


  • include/exclude:包含和排除的数据类型信息


返回的信息包含:


  • 非空值的数量count;特例:math字段中有一个空值


  • 均值mean


  • 标准差std


  • 最小值min


  • 最大值max


  • 25%、50%、75%分位数


df.describe()



添加了参数后的情况,我们发现:


  • sex字段的相关信息也被显示出来


  • 显示的信息更丰富,多了unique、top、freq等等



非空值数量count


返回的是每个字段中非空值的数量


In [5]:


df.count()


Out[5]:


sex5 age5 chinese5 math4#包含一个空值 english5 dtype:int64


求和sum


In [6]:


df.sum()


在这里我们发现:如果字段是object类型的,sum函数的结果就是直接将全部取值拼接起来


Out[6]:


sexmalemalefemalefemalemale#拼接 age121#相加求和 chinese520 math390.0 english490 dtype:object


最大值max


In [7]:


df.max()


针对字符串的最值(最大值或者最小值),是根据字母的ASCII码大小来进行比较的:


  • 先比较首字母的大小


  • 首字母相同的话,再比较第二个字母


Out[7]:


sexmale age26 chinese120 math120.0 english130 dtype:object


最小值min


和max函数的求解是类似的:


In [8]:


df.min()


Out[8]:


sexfemale age22 chinese90 math80.0 english80 dtype:object


分位数quantile


返回指定位置的分位数


In [9]:


df.quantile(0.2)


Out[9]:


age23.6 chinese98.0 math86.0 english88.0 Name:0.2,dtype:float64


In [10]:


df.quantile(0.25)


Out[10]:


age24.0 chinese100.0 math87.5 english90.0 Name:0.25,dtype:float64


In [11]:


df.quantile(0.75)


Out[11]:


age25.0 chinese110.0 math105.0 english100.0 Name:0.75,dtype:float64


通过箱型图可以展示一组数据的25%、50%、75%的中位数:


In [12]:


plt.figure(figsize=(12,6))#设置画布的尺寸 plt.boxplot([df["age"],df["chinese"],df["english"]], labels=["age","chinese","english"], #vert=False, showmeans=True, patch_artist=True, boxprops={'color':'orangered','facecolor':'pink'} #showgrid=True ) plt.show()



箱型图的具体展示信息:



均值mean


一组数据的平均值


In [13]:


df.mean()


Out[13]:


age24.2 chinese104.0 math97.5 english98.0 dtype:float64


通过下面的例子我们发现:如果字段中存在缺失值(math存在缺失值),此时样本的个数会自动忽略缺失值的总数


In [14]:


390/4#个数不含空值


Out[14]:


97.5


中值/中位数median


比如:1,2,3,4,5 的中位数就是3


再比如:1,2,3,4,5,6 的中位数就是 3+4 = 3.5


In [15]:


df.median()


Out[15]:


age24.0 chinese100.0 math95.0 english90.0 dtype:float64


众数mode


一组数据中出现次数最多的数


In [16]:


df.mode()


Out[16]:



最大值索引idmax


idxmax() 返回的是最大值的索引


In [17]:


df["age"].idxmax()


Out[17]:


3


In [18]:


df["chinese"].idxmin()


Out[18]:


4


不能字符类型的字段使用该函数,Pandas不支持:


In [19]:


df["sex"].idxmax()




相关文章
|
29天前
|
Serverless 数据处理 索引
Pandas中的shift函数:轻松实现数据的前后移动
Pandas中的shift函数:轻松实现数据的前后移动
112 0
|
29天前
|
数据采集 数据可视化 数据挖掘
Pandas函数大合集:数据处理神器一网打尽!
Pandas函数大合集:数据处理神器一网打尽!
31 0
|
29天前
|
数据处理 Python
Pandas中的map函数应用
Pandas中的map函数应用
14 2
|
29天前
|
数据处理 Python
Pandas快速统计重复值的2种方法
Pandas快速统计重复值的2种方法
90 1
|
29天前
|
数据挖掘 Python
pandas中的groupby函数应用
pandas中的groupby函数应用
14 0
pandas中的groupby函数应用
|
29天前
|
数据处理 索引 Python
Pandas中的filter函数:有点鸡肋
Pandas中的filter函数:有点鸡肋
13 0
|
29天前
|
Python
pandas中的applymap函数应用
pandas中的applymap函数应用
12 0
|
29天前
|
Python
Pandas中的apply函数应用
Pandas中的apply函数应用
14 0
|
2月前
|
SQL JSON 数据可视化
Pandas库常用方法、函数集合
Pandas库常用方法、函数集合
|
2月前
|
存储 索引 Python
高效的5个pandas函数,你都用过吗?
高效的5个pandas函数,你都用过吗?