サンフランシスコ ベイエリアのレンタサイクル "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()
| Duration | End Station | |
|---|---|---|
| Start Station | ||
| San Francisco Ferry Building (Harry Bridges Plaza) | 13338 | 13338 |
| San Francisco Caltrain (Townsend St at 4th St) | 12320 | 12320 |
| San Francisco Caltrain Station 2 (Townsend St at 4th St) | 11890 | 11890 |
| The Embarcadero at Sansome St | 11861 | 11861 |
| Market St at 10th St | 11577 | 11577 |
借りたステーション(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 Station | start_count | |
|---|---|---|
| 0 | San Francisco Ferry Building (Harry Bridges Pl... | 13338 |
| 1 | San Francisco Caltrain (Townsend St at 4th St) | 12320 |
| 2 | San Francisco Caltrain Station 2 (Townsend St... | 11890 |
| 3 | The Embarcadero at Sansome St | 11861 |
| 4 | Market St at 10th St | 11577 |
reset_index()でインデックスを振り直して列名の階層を解除、dropで"End Station"列を削除、renameで列名を変更しました。
同様に返却ステーション(End Station)でも利用回数を計算しました。
| End Station | end_count | |
|---|---|---|
| 0 | San Francisco Caltrain (Townsend St at 4th St) | 17225 |
| 1 | San Francisco Ferry Building (Harry Bridges Pl... | 15770 |
| 2 | San Francisco Caltrain Station 2 (Townsend St... | 13532 |
| 3 | Montgomery St BART Station (Market St at 2nd St) | 13115 |
| 4 | The Embarcadero at Sansome St | 13038 |
借りたステーション、返却したステーション、それぞれ利用回数が多い順に並んでいます。全体に返却ステーションの方が回数が多いようです。あちこちから目的地に向かって集まっているというイメージでしょうか。
common_list=pd.concat([common_start,common_end],axis=1) common_list.head()
| Start Station | start_count | End Station | end_count | |
|---|---|---|---|---|
| 0 | San Francisco Ferry Building (Harry Bridges Pl... | 13338 | San Francisco Caltrain (Townsend St at 4th St) | 17225 |
| 1 | San Francisco Caltrain (Townsend St at 4th St) | 12320 | San Francisco Ferry Building (Harry Bridges Pl... | 15770 |
| 2 | San Francisco Caltrain Station 2 (Townsend St... | 11890 | San Francisco Caltrain Station 2 (Townsend St... | 13532 |
| 3 | The Embarcadero at Sansome St | 11861 | Montgomery St BART Station (Market St at 2nd St) | 13115 |
| 4 | Market St at 10th St | 11577 | The Embarcadero at Sansome St | 13038 |
借りたステーションと返却したステーションの情報を結合します。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()
| Station | start_count | end_count | |
|---|---|---|---|
| 0 | San Francisco Ferry Building (Harry Bridges Pl... | 13338 | 15770 |
| 1 | San Francisco Caltrain (Townsend St at 4th St) | 12320 | 17225 |
| 2 | San Francisco Caltrain Station 2 (Townsend St... | 11890 | 13532 |
| 3 | The Embarcadero at Sansome St | 11861 | 13038 |
| 4 | Market St at 10th St | 11577 | 11022 |
借りたステーションと返却ステーションの利用回数情報をステーション名で結合(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 Station | San 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 St | Market St at 10th St | Powell St BART Station (Market St at 4th St) | Berry St at 4th St | Steuart St at Market St | Powell St BART Station (Market St at 5th St) |
|---|---|---|---|---|---|---|---|---|---|---|
| Start Station | ||||||||||
| San Francisco Ferry Building (Harry Bridges Plaza) | 553.0 | 304.0 | 104.0 | 329.0 | 2842.0 | 145.0 | 249.0 | 1381.0 | 28.0 | 216.0 |
| San Francisco Caltrain (Townsend St at 4th St) | 52.0 | 734.0 | 11.0 | 459.0 | 361.0 | 149.0 | 193.0 | 10.0 | 268.0 | 221.0 |
| San Francisco Caltrain Station 2 (Townsend St at 4th St) | 11.0 | 580.0 | 48.0 | 548.0 | 191.0 | 618.0 | 328.0 | 10.0 | 226.0 | 241.0 |
| The Embarcadero at Sansome St | 453.0 | 1639.0 | 68.0 | 296.0 | 486.0 | 63.0 | 358.0 | 323.0 | 1764.0 | 279.0 |
| Market St at 10th St | 200.0 | 273.0 | 1146.0 | 726.0 | 69.0 | 144.0 | 825.0 | 95.0 | 179.0 | 661.0 |
| Montgomery St BART Station (Market St at 2nd St) | 985.0 | 588.0 | 295.0 | 91.0 | 346.0 | 434.0 | 123.0 | 363.0 | 213.0 | 117.0 |
| Berry St at 4th St | 16.0 | 1671.0 | 10.0 | 430.0 | 459.0 | 122.0 | 161.0 | 178.0 | 642.0 | 233.0 |
| Howard St at Beale St | 1181.0 | 174.0 | 232.0 | 66.0 | 581.0 | 267.0 | 139.0 | 381.0 | 84.0 | 93.0 |
| Powell St BART Station (Market St at 4th St) | 664.0 | 356.0 | 315.0 | 170.0 | 339.0 | 607.0 | 146.0 | 187.0 | 230.0 | 134.0 |
| Steuart St at Market St | 707.0 | 27.0 | 149.0 | 121.0 | 1290.0 | 54.0 | 145.0 | 839.0 | 86.0 | 83.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