1、ython数据分析单车租赁数据分析租赁情况ython数据分析,单车租赁数据分析, 租赁情况本节选取自行车的租赁数据;利用numpyx pandas, matplotlib三个库#数 据清洗后,做数据分析,研究时间段与自行车租赁的关系。数据来源本节以自行车的租赁数据为例,数据来源于网络,利用时间序列分析的方法,通 过可视化技术,分析自行车租赁随时间及天气变化的分布情况,其中datetime, season、holiday, workingday、weather、temp、atemp、humidity, windspeed, casual . registered, count字段分别代表租赁时间
2、、季节、是 否为假期、是否为工作日、天气数字越大,天气越差、temp atemp气温、湿 度、风速、普通用户、注册用户、租赁自行车数量。import numpy as npimport pandas as pdimport matplotlib.pyplot as pit%matplotlib inlinebike = pd read_csv (open (r 1 D: python 数据分析#bike csv1)bike head()datetimeseasonholidayworkingdoyweathertempatemphumiditywindspeedcasualregistered
3、count02011-01-01 00:00 0010019.8414.39581003131612011-01-01 01 00 0010019 0213.63580008324022011-01-01 02:00:0010019.0213.63580005273232011-01-01 03 00 0010019 8414 39575003101342011-01-01 04:00 0010019.8414.3957500011问题探索研究时间段与自行车租赁的关系情况。bike isnull() sum()datetime 0 season 0 holiday 0 vorkingday 0
4、 weather 0 t emp 0 atemp 0 humidity 0 windspeed 0 casual 0 registered 0 count 0dtype: int64查看缺失值,无缺失值。bike dtypesdatetimeobjectseasonint64holidayint64workingdayint64weatherint64t empfloat64a tempfloat64humidityint64windspeedfloat64casualint64registeredint64countint64dtype: object查看数据类型,datetime字段不是时
5、间数据类型。bikedatetime = pd to_datetime(bike* daterime 1)bike dtypes将to_datetime函数转换为datetime类数据。bike = bike set_index (datetime,) datetine 殳设为 DwtaFrame J索引,成为时间序列竅据bike head()datetimeseasonholidayworkin gdayweathertempatemphumiditywindspeedcasualregisteredcount2011.01X)1 00:00:0010019 8414 395810.0313
6、162011 01-01 01:00:00100190213635800.0832402011 01 01 02:00:00100190213635800.0527322011-01-01 03:00:0010019 8414395750.0310132011-014)1 04:00:0010019 8414395750.0011bike indexbike tail ()datetimeseasonholidayworkingdayweathertempa temphumiditywindspeedcasualregisteredcount2012-12-19 19:00:00401115
7、5819 6955026.002773293362012-12-19 20:00:00401114 76仃4255715 0013102312412012-12-19 21:00:00401113 9415 9106115.001341641682012-12-19 22:00:00401113.9417.425616.0032121171292012-12-19 23:00:00401113.1216665668.998148488数据探索y_bike = bikegroupby (lambda x: x.year)mean () ;y_bikecount *2011 144.223349N
8、ame: count, dtype: float64y_bike count1 . plot (kind=f bar1 )二:很1状1?1rj82012年的租赁数据多于2011年数据。mbike = bike resample(, kind=1 period)mean() 型为时期类型*重采样到月份,类m_bike head ()datetimeseasonholidayworkingdaywoaiheitempatomphumiditywindspeedcasualroglsteredcount20114)1100 05568406126291 4408358.63378210 767981
9、6 308S8513 7498304.66893349 98607964 64S0122011-02100.0000000 7331841 37892411.33107613 99992253 58071715 5092988.46636865 174883736412562011 031.0O.OOOCOO0.7354261.46636814.06318416 89559455.92376716.03386617.73542669.11435086.8497762011-04200 0527470 6307691 61978017.77687921 23983566 28571415 844
10、23426.87692384 149451111.0263742011-05200 00000007368421 52850921.52859625 45514371 42105312 35535834791667140 017544174.809211figr axes = pit. subplots (2, 1) #两彳亍一列m_bike 20111 1 count1 .plot (ax=axes 0 , sharex=True) #贡献 X $fl|m bike12012 * counr1 .plot(ax=axes1)datetime2011年和2012年的趋势大致相同,前几个月逐渐增
11、加,到5、6月份到达峰 值,再到9月份后逐渐减少。bikeday1 = bike index.daybike ,hour, = bike. index .hour # 单独存储 L 4川J V 敌;bike head()datetimeseasonholidayworklngdayweathertempatemphumiditywindspeedcasualregisteredcountdayhour2011-01-01 00:00:0010019 8414 395810031316102011-01-01 01:00:00100190213 635800083240112011-01-01
12、02:00:0010019 0213 635800052732122011 01 01 03:00:0010019 8414 395750031013132011 01 01 04:00:0010019 8414.395750.001114d_bike = bike gzoupby J day1) count1 mean ()冬刈 day L2 刃1纟荒门 d bikeday1180.3339132183.9109953194. 6963354195.7055755189.7652176189.8601407183.7735198179. 0418129187.89739110195.1835
13、6611195.67957712190.67539313194. 16027914195.82926815201.52787516191.35365917205.66087018192.60568419192.311847Warne:count, dtype: fl oat64d_bike .plot () # I彳?毎T租赁数彷佗dayh_bike = bike .group by (hour) * count mean () f hour :,:/;*h bike055.1384Q2133.859031222-899554311.75750646.407240519. 767699676.
14、2593417213.1164848362.7692319221-78022010175.09230811210. 67472512256.50877213257.78728114243. 44298215254. 29824616316. 37280717468.76535118430.85964919315. 27850920228.51754421173. 37061422133.5767542389-508772Name:count, dtyji_bike.plot() #hourfloat64自行车每小时租赁数分布hour图中有明显的两个峰值,都是上下班时间段,并且晩上的峰值更高。w
15、ork_bike = bike.groupby(,wozkingday) * count1mean()work_bike workingday 1、刃丨:玄Lworkingday0 188.5066211 193- 011873Name: count, dtype: float64work_bike plot(kind=bar 1)workingday天气越差,自行车租赁数越少。weather_bike = bikegzoupby(,weather) 1 count1mean()we a ther bike 上we a ther /I / /j:丨无 i Iweather1205. 2367912178.9555403118.8463334164.000000Name: count, dtype: float64we a thejbike plot (kind=,bar )weather天气越差,自行车租赁数越少,但在极端天气情况下却略有上升。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1