一、pandas的常用统计函数:
1、汇总类
2、唯一去重和按值计数
3、相关系数和方差
二、汇总类
1、df.describe():提取所有数字列的统计结果
1)查看全部书之列
import pandas as pd df=pd.read_csv('./beijing_tianqi/beijing_tianqi_2018.csv') #读取文件 # print(df.head()) df['bWendu']=df['bWendu'].str.replace('℃','').astype('int32') #去掉°C符号 df['yWendu']=df['yWendu'].str.replace('℃','').astype('int32') # print(df.head()) print(df.describe()) ''' bWendu yWendu aqi aqiLevel count 365.000000 365.000000 365.000000 365.000000 #计数 mean 18.665753 8.358904 82.183562 2.090411 #均值 std 11.858046 11.755053 51.936159 1.029798 #标准差 min -5.000000 -12.000000 21.000000 1.000000 25% 8.000000 -3.000000 46.000000 1.000000 50% 21.000000 8.000000 69.000000 2.000000 75% 29.000000 19.000000 104.000000 3.000000 max 38.000000 27.000000 387.000000 6.000000 ''' print(type(df.describe()))
'''
<class 'pandas.core.frame.DataFrame'>
'''
2)查看某一个数值列
print(df['bWendu'].describe()) print('______________________') print(df['bWendu'].mean()) ''' count 365.000000 mean 18.665753 std 11.858046 min -5.000000 25% 8.000000 50% 21.000000 75% 29.000000 max 38.000000 Name: bWendu, dtype: float64 ______________________ 18.665753424657535 '''
三、唯一去重和按值计数:一般不用于数值列,而是枚举、分类列
1、唯一去重:df[].unique()
#去重函数 print(df.shape) print('__________________') print(df['fengxiang'].unique()) #查看‘风向列’有多少种取值 ''' (365, 9) __________________ ['东北风' '北风' '西北风' '西南风' '南风' '东南风' '东风' '西风'] '''
2、按值计数:df[].value_counts计算不同取值各有多少个
#按值计数 print(df['fengxiang'].value_counts()) ''' 南风 92 西南风 64 北风 54 西北风 51 东南风 46 东北风 38 东风 14 西风 6 Name: fengxiang, dtype: int64 ''''
四、相关系数和协方差
协方差:衡量同向反向的程度,如果为正,说明同向变化,数值越大说明同向程度越高;如果为负数,说明是相反变化,数值越小说明反向长度越高
相关系数:衡量相似系数,如果为1,说明两个变量变化时正向相似度最大,如果为-1,说明变量变化的反向相似度最大
print(df.cov()) #协方差矩阵 print('----------------------------------') print(df.corr()) #相关系数矩阵 print('----------------------------------') print('最高温与空气质量的相关系数为{}:'.format(df['aqi'].corr(df['bWendu']))) print('----------------------------------') print('最低温与空气质量的相关系数为:{}'.format(df['aqi'].corr(df['yWendu']))) print('----------------------------------') print('温差与空气质量的相关系数为:{}'.format(df['aqi'].corr(df['bWendu']-df['yWendu']))) ''' bWendu yWendu aqi aqiLevel bWendu 140.613247 135.529633 47.462622 0.879204 yWendu 135.529633 138.181274 16.186685 0.264165 aqi 47.462622 16.186685 2697.364564 50.749842 aqiLevel 0.879204 0.264165 50.749842 1.060485 ---------------------------------- bWendu yWendu aqi aqiLevel bWendu 1.000000 0.972292 0.077067 0.071999 yWendu 0.972292 1.000000 0.026513 0.021822 aqi 0.077067 0.026513 1.000000 0.948883 aqiLevel 0.071999 0.021822 0.948883 1.000000 ---------------------------------- 最高温与空气质量的相关系数为0.07706705916811077: ---------------------------------- 最低温与空气质量的相关系数为:0.02651328267296879 ---------------------------------- 温差与空气质量的相关系数为:0.21652257576382047 '''