本文使用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_id | starttime | stoptime | bikeid | tripduration | from_station_id | from_station_name | to_station_id | to_station_name | usertype | gender | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 9080551 | 3/31/2016 23:53 | 4/1/2016 0:07 | 155 | 841 | 344 | Ravenswood Ave & Lawrence Ave | 458 | Broadway & Thorndale Ave | Subscriber | Male |
1 | 9080550 | 3/31/2016 23:46 | 3/31/2016 23:57 | 4831 | 649 | 128 | Damen Ave & Chicago Ave | 213 | Leavitt St & North Ave | Subscriber | Male |
2 | 9080549 | 3/31/2016 23:42 | 3/31/2016 23:46 | 4232 | 210 | 350 | Ashland Ave & Chicago Ave | 210 | Ashland Ave & Division St | Subscriber | Male |
3 | 9080548 | 3/31/2016 23:37 | 3/31/2016 23:55 | 3464 | 1045 | 303 | Broadway & Cornelia Ave | 458 | Broadway & Thorndale Ave | Subscriber | Male |
4 | 9080547 | 3/31/2016 23:33 | 3/31/2016 23:37 | 1750 | 202 | 334 | Lake Shore Dr & Belmont Ave | 329 | Lake Shore Dr & Diversey Pkwy | Subscriber | Male |
#首先,可以通过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 | |
---|---|
count | 10000.000000 |
mean | 807.512700 |
std | 2232.133671 |
min | 62.000000 |
25% | 352.000000 |
50% | 571.500000 |
75% | 912.000000 |
max | 81702.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_id | starttime | stoptime | bikeid | from_station_id | from_station_name | to_station_id | to_station_name | usertype | gender | |
---|---|---|---|---|---|---|---|---|---|---|
count | 10000 | 10000 | 10000 | 10000 | 10000 | 10000 | 10000 | 10000 | 10000 | 8975 |
unique | 10000 | 1538 | 1547 | 2817 | 431 | 431 | 430 | 430 | 2 | 2 |
top | 9070628 | 3/31/2016 17:18 | 3/30/2016 17:09 | 4341 | 91 | Clinton St & Washington Blvd | 174 | Canal St & Madison St | Subscriber | Male |
freq | 1 | 29 | 32 | 13 | 184 | 184 | 222 | 222 | 8977 | 7047 |
#可以看出,两种类型的数据,描述性统计的描述方式是不同的
#除了以上两种最基本的方法外,pandas还提供了计算均值,标准差,方差,分位数等一系列专门的方法,
#以计算均值为例,可以使用如下方法
ridership.var()
tripduration 4.982421e+06
dtype: float64