pandas描述性统计 (1)

本文使用IPython自动生成的md文件导入

#使用pandas完成描述性统计
#通常拿到一组数据的第一步便是对数据的认识与探索,在这个过程中,描述性统计能够帮助我们快速地对数据有一个大致的了解
#本节主要讲解如何利用pandas实现数据的描述性统计
import pandas as pd
import numpy as np
type_specified={"trip_id":"object","bikeid":"object","from_station_id":"object","to_station_id":"object",
               "hour":"object","weekday":"object"}
ridership=pd.read_csv(r"E:\chicago_bikes_data\txt&csv\ridership_2016.txt",
                      nrows=10000,dtype=type_specified,usecols=range(1,12))
#ridership数据集是芝加哥公共自行车DivvyTrips使用记录的一部分,
ridership.head()
.dataframe thead tr:only-child th { text-align: right; } .dataframe thead th { text-align: left; } .dataframe tbody tr th { vertical-align: top; }
trip_idstarttimestoptimebikeidtripdurationfrom_station_idfrom_station_nameto_station_idto_station_nameusertypegender
090805513/31/2016 23:534/1/2016 0:07155841344Ravenswood Ave & Lawrence Ave458Broadway & Thorndale AveSubscriberMale
190805503/31/2016 23:463/31/2016 23:574831649128Damen Ave & Chicago Ave213Leavitt St & North AveSubscriberMale
290805493/31/2016 23:423/31/2016 23:464232210350Ashland Ave & Chicago Ave210Ashland Ave & Division StSubscriberMale
390805483/31/2016 23:373/31/2016 23:5534641045303Broadway & Cornelia Ave458Broadway & Thorndale AveSubscriberMale
490805473/31/2016 23:333/31/2016 23:371750202334Lake Shore Dr & Belmont Ave329Lake Shore Dr & Diversey PkwySubscriberMale
#首先,可以通过info方法,获取数据集的基本信息,该方法可以简要描述数据各列的类型,非缺失的字段数目
ridership.info()
#info()方法返回的信息,显示gender列存在缺失值,除过tripduration列是数值列外,其他列都是字符类型(object,和str类型是同一个意思)
#使用describe能够方便的对Series,DataFrame类型的数据完成简单的统计
#在此需要注意的是,有的数据列类型为数值,有的则为字符类型,describe函数对这两种类型的数据描述统计分析也是不同的
#describe函数默认只对数值型数据进行描述性统计,为了对字符型数据进行描述性统计,可设置参数include
ridership.describe()
.dataframe thead tr:only-child th { text-align: right; } .dataframe thead th { text-align: left; } .dataframe tbody tr th { vertical-align: top; }
tripduration
count10000.000000
mean807.512700
std2232.133671
min62.000000
25%352.000000
50%571.500000
75%912.000000
max81702.000000
ridership.describe(include=["object"])
.dataframe thead tr:only-child th { text-align: right; } .dataframe thead th { text-align: left; } .dataframe tbody tr th { vertical-align: top; }
trip_idstarttimestoptimebikeidfrom_station_idfrom_station_nameto_station_idto_station_nameusertypegender
count1000010000100001000010000100001000010000100008975
unique1000015381547281743143143043022
top90706283/31/2016 17:183/30/2016 17:09434191Clinton St & Washington Blvd174Canal St & Madison StSubscriberMale
freq129321318418422222289777047
#可以看出,两种类型的数据,描述性统计的描述方式是不同的
#除了以上两种最基本的方法外,pandas还提供了计算均值,标准差,方差,分位数等一系列专门的方法,
#以计算均值为例,可以使用如下方法
ridership.var()
tripduration    4.982421e+06
dtype: float64
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值