ラベル groupby の投稿を表示しています。 すべての投稿を表示
ラベル groupby の投稿を表示しています。 すべての投稿を表示

2018年5月21日月曜日

サンフランシスコのレンタサイクルのデータを見てみる その2。よく使われているステーションを調べる(python、pandas、groupby、pivot_table)

edX UCバークレー データサイエンス基礎講座(Foundation of Data Science)の演習、前回の続きです。

サンフランシスコ ベイエリアのレンタサイクル "Ford Go Bike"の利用状況を把握する。
  • 利用時間の傾向、分布
  • よく使われているステーション
  • ステーションの地図の可視化

今回はこのうち、よく使われているステーションを調べてみます。

元データ
サンフランシスコ ベイエリア レンタサイクル Ford Go Bike
https://www.fordgobike.com/
システムデータ
https://s3.amazonaws.com/fordgobike-data/index.html
2017年のトリップデータ(このデータを使います)
https://s3.amazonaws.com/fordgobike-data/2017-fordgobike-tripdata.csv

import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt

trips = pd.read_csv('tripdata.csv')

duration= trips[["duration_sec","start_station_name","end_station_name"]]
duration=duration.rename(columns={'duration_sec': 'Duration','start_station_name': 'Start Station','end_station_name':"End Station"})

データ読み込み、列名変更。ここまで前回と同じ。

commute = duration[duration["Duration"] <= 2000]
commute.head()

前回見たとおり、利用時間が2000秒以上は少ないので今回は2000秒以下のみを対象にします。

common_start=commute.groupby(["Start Station"]).count().sort_values(by=["Duration"],ascending=False).reset_index().drop("End Station",axis=1)
common_start=common_start.rename(columns={'Duration':'start_count'})
common_start.head()
DurationEnd Station
Start Station
San Francisco Ferry Building (Harry Bridges Plaza)1333813338
San Francisco Caltrain (Townsend St at 4th St)1232012320
San Francisco Caltrain Station 2 (Townsend St at 4th St)1189011890
The Embarcadero at Sansome St1186111861
Market St at 10th St1157711577

借りたステーション(Start Station)でグループ化(groupby)し、回数を集計(count)して並べ替え(sort)。列名が階層化されています。

common_start=common_start.reset_index().drop("End Station",axis=1).rename(columns={'Duration':'start_count'})
common_start.head()
Start Stationstart_count
0San Francisco Ferry Building (Harry Bridges Pl...13338
1San Francisco Caltrain (Townsend St at 4th St)12320
2San Francisco Caltrain Station 2 (Townsend St...11890
3The Embarcadero at Sansome St11861
4Market St at 10th St11577


reset_index()でインデックスを振り直して列名の階層を解除、dropで"End Station"列を削除、renameで列名を変更しました。

同様に返却ステーション(End Station)でも利用回数を計算しました。
End Stationend_count
0San Francisco Caltrain (Townsend St at 4th St)17225
1San Francisco Ferry Building (Harry Bridges Pl...15770
2San Francisco Caltrain Station 2 (Townsend St...13532
3Montgomery St BART Station (Market St at 2nd St)13115
4The Embarcadero at Sansome St13038


借りたステーション、返却したステーション、それぞれ利用回数が多い順に並んでいます。全体に返却ステーションの方が回数が多いようです。あちこちから目的地に向かって集まっているというイメージでしょうか。

common_list=pd.concat([common_start,common_end],axis=1)
common_list.head() 
Start Stationstart_countEnd Stationend_count
0San Francisco Ferry Building (Harry Bridges Pl...13338San Francisco Caltrain (Townsend St at 4th St)17225
1San Francisco Caltrain (Townsend St at 4th St)12320San Francisco Ferry Building (Harry Bridges Pl...15770
2San Francisco Caltrain Station 2 (Townsend St...11890San Francisco Caltrain Station 2 (Townsend St...13532
3The Embarcadero at Sansome St11861Montgomery St BART Station (Market St at 2nd St)13115
4Market St at 10th St11577The Embarcadero at Sansome St13038


借りたステーションと返却したステーションの情報を結合します。pd.concat、axis=1で列の横方向結合です。こちらも借りたステーション、返却したステーション、それぞれ利用回数が多い順に並んでいます。

common_station=common_start.set_index('Start Station').join(common_end.set_index('End Station')).reset_index()
common_station=common_station.rename(columns={'Start Station':'Station'})
common_station.head()
Stationstart_countend_count
0San Francisco Ferry Building (Harry Bridges Pl...1333815770
1San Francisco Caltrain (Townsend St at 4th St)1232017225
2San Francisco Caltrain Station 2 (Townsend St...1189013532
3The Embarcadero at Sansome St1186113038
4Market St at 10th St1157711022


借りたステーションと返却ステーションの利用回数情報をステーション名で結合(join)してみました。joinはindexでの結合になるので、set_indexでStart Station、End Stationをそれぞれindexに設定してから結合し、借りたステーションの利用回数順に並べ替えています。

これを見ると、借りたステーションと返したステーションで利用回数にかなり違いがあります。返却が多いステーションから借りる方が多いステーションへ自転車を移動する必要がありそうです。

commute_pivot=pd.pivot_table(commute,index='Start Station', columns='End Station',values='Duration',aggfunc="count")
commute_pivot.loc[common_start["Start Station"],common_end["End Station"]].iloc[0:10,0:10]
End StationSan Francisco Caltrain (Townsend St at 4th St)San Francisco Ferry Building (Harry Bridges Plaza)San Francisco Caltrain Station 2 (Townsend St at 4th St)Montgomery St BART Station (Market St at 2nd St)The Embarcadero at Sansome StMarket St at 10th StPowell St BART Station (Market St at 4th St)Berry St at 4th StSteuart St at Market StPowell St BART Station (Market St at 5th St)
Start Station
San Francisco Ferry Building (Harry Bridges Plaza)553.0304.0104.0329.02842.0145.0249.01381.028.0216.0
San Francisco Caltrain (Townsend St at 4th St)52.0734.011.0459.0361.0149.0193.010.0268.0221.0
San Francisco Caltrain Station 2 (Townsend St at 4th St)11.0580.048.0548.0191.0618.0328.010.0226.0241.0
The Embarcadero at Sansome St453.01639.068.0296.0486.063.0358.0323.01764.0279.0
Market St at 10th St200.0273.01146.0726.069.0144.0825.095.0179.0661.0
Montgomery St BART Station (Market St at 2nd St)985.0588.0295.091.0346.0434.0123.0363.0213.0117.0
Berry St at 4th St16.01671.010.0430.0459.0122.0161.0178.0642.0233.0
Howard St at Beale St1181.0174.0232.066.0581.0267.0139.0381.084.093.0
Powell St BART Station (Market St at 4th St)664.0356.0315.0170.0339.0607.0146.0187.0230.0134.0
Steuart St at Market St707.027.0149.0121.01290.054.0145.0839.086.083.0


pivot_tableで借りたステーションと返却したステーションの対応を見てみます。行(index)と列(columns)をそれぞれ選択、aggfuncで集計方法をします。.locで行、列の並び順を利用回数の多い順に並んだシリーズで指定し、ilocでトップ10のみを表示させました。

San Francisco Ferry Buildingで借りた人がThe Embarcadero at Sansome Stに返した回数は2842回、Berry St at 4th Stに返した回数は1381回です。けっこうバラつきがあり、自転車を過不足なく各ステーションに配置するのは大変そうです。

つづく

参考:
edx UC Berkeley Foundations of Data Science(UCバークレー データサイエンス基礎講座)
ttps://www.edx.org/professional-certificate/berkeleyx-foundations-of-data-science
Computational Thinking with Python(pythonによるプログラミング的思考)
https://www.edx.org/course/foundations-data-science-computational-uc-berkeleyx-data8-1x

サンフランシスコ ベイエリア レンタサイクル Ford Go Bike
https://www.fordgobike.com/
システムデータ
https://s3.amazonaws.com/fordgobike-data/index.html

サンフランシスコのレンタサイクルのデータを見てみる(基本統計量、python、pandas、ヒストグラム)
https://eneprog.blogspot.com/2018/05/pythonpandas_17.html

プログラミング学習:edx UCバークレー データサイエンス基礎講座の紹介(python)
https://eneprog.blogspot.com/2018/04/edx-uc-python.html
プログラミング学習:pandasでweb上の表を取得する。(python)
https://eneprog.blogspot.com/2018/04/pandaswebpython.html プログラミング学習:各国の女性の平均就学期間をグラフ化する。(python,pandas)
https://eneprog.blogspot.com/2018/05/pythonpandas.html
//SyntaxHighlighter CDNより https://cdnjs.com/libraries/SyntaxHighlighter // 対応言語