サンフランシスコ ベイエリアのレンタサイクル "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
・foliumのインストール
!pip install folium
地図上への可視化はfoliumを使います。インストールされていなければpip installで入れてください。ちなみに今回インストールしたバージョンは、folium 0.5.0でした。他の方のブログ記事等を見ると、関数などが大幅に違っているようなのでバージョンをご確認ください。
・インポートとデータの読み込み
import numpy as np import pandas as pd import matplotlib import matplotlib.pyplot as plt import folium
trips = pd.read_csv('tripdata.csv')
trips.head()| duration_sec | start_time | end_time | start_station_id | start_station_name | start_station_latitude | start_station_longitude | end_station_id | end_station_name | end_station_latitude | end_station_longitude | bike_id | user_type | member_birth_year | member_gender | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 80110 | 2017-12-31 16:57:39.6540 | 2018-01-01 15:12:50.2450 | 74 | Laguna St at Hayes St | 37.776435 | -122.426244 | 43 | San Francisco Public Library (Grove St at Hyde... | 37.778768 | -122.415929 | 96 | Customer | 1987.0 | Male |
| 1 | 78800 | 2017-12-31 15:56:34.8420 | 2018-01-01 13:49:55.6170 | 284 | Yerba Buena Center for the Arts (Howard St at ... | 37.784872 | -122.400876 | 96 | Dolores St at 15th St | 37.766210 | -122.426614 | 88 | Customer | 1965.0 | Female |
| 2 | 45768 | 2017-12-31 22:45:48.4110 | 2018-01-01 11:28:36.8830 | 245 | Downtown Berkeley BART | 37.870348 | -122.267764 | 245 | Downtown Berkeley BART | 37.870348 | -122.267764 | 1094 | Customer | NaN | NaN |
| 3 | 62172 | 2017-12-31 17:31:10.6360 | 2018-01-01 10:47:23.5310 | 60 | 8th St at Ringold St | 37.774520 | -122.409449 | 5 | Powell St BART Station (Market St at 5th St) | 37.783899 | -122.408445 | 2831 | Customer | NaN | NaN |
| 4 | 43603 | 2017-12-31 14:23:14.0010 | 2018-01-01 02:29:57.5710 | 239 | Bancroft Way at Telegraph Ave | 37.868813 | -122.258764 | 247 | Fulton St at Bancroft Way | 37.867789 | -122.265896 | 3167 | Subscriber | 1997.0 | Female |
データの読み込みと確認。ここまで前回と同じです。start、endステーションそれぞれに緯度(Latitude)・経度(Longitude)の情報が入っています。データ数は約52万件です。ステーションはstart、end共有なので、今回はstartステーションの情報を使うことにします。
・データの抽出、重複除去
stations=trips[["start_station_name","start_station_latitude","start_station_longitude"]]
stations=stations.rename(columns={'start_station_name': 'station','start_station_latitude':"lat","start_station_longitude":"long"})
stations=stations.drop_duplicates()
stations.head()
| station | lat | long | |
|---|---|---|---|
| 0 | Laguna St at Hayes St | 37.776435 | -122.426244 |
| 1 | Yerba Buena Center for the Arts (Howard St at ... | 37.784872 | -122.400876 |
| 2 | Downtown Berkeley BART | 37.870348 | -122.267764 |
| 3 | 8th St at Ringold St | 37.774520 | -122.409449 |
| 4 | Bancroft Way at Telegraph Ave | 37.868813 | -122.258764 |
tripsからステーション名、緯度、経度を取り出し、renameで列名を変更、drop_duplicates()で重複を取り除きました。272件のステーション情報が取得できました。
・前回作ったデータの確認
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 |
前回作ったデータフレーム"common_start"を使います。開始ステーション名と利用回数が入っています。
・データベースの結合、並べ替え
stations_startcount=pd.merge(stations,common_start, left_on='station', right_on='Start Station').drop("Start Station",axis=1)
stations_startcount=stations_startcount.sort_values(by=["start_count"],ascending=False).set_index(["station"])
stations_startcount.head()
| lat | long | start_count | |
|---|---|---|---|
| station | |||
| San Francisco Ferry Building (Harry Bridges Plaza) | 37.795392 | -122.394203 | 13338 |
| San Francisco Caltrain (Townsend St at 4th St) | 37.776598 | -122.395282 | 12320 |
| San Francisco Caltrain Station 2 (Townsend St at 4th St) | 37.776639 | -122.395526 | 11890 |
| The Embarcadero at Sansome St | 37.804770 | -122.403234 | 11861 |
| Market St at 10th St | 37.776619 | -122.417385 | 11577 |
pd.mergeで2つのデータフレームを結合します。結合する列名がstation、Start Stationと異なっているので、left_on、right_onで指定します。結合後には両方の列が残るので、片方(Start Station)をdropで削除しました。sort_valuesで利用回数の多い順に並べ替えてデータは完成。
いよいよ地図に表示させます。下記ページが大変参考になりました。
folium quickstart・foliumでのマップ表示とマーカーの配置
け日記 Python: foliumで地図を描画する
Qiita Folium: Pythonでデータを地図上に可視化
map_osm = folium.Map(location=[37.792714,-122.248780],zoom_start=8)
for i, r in stations_startcount[0:100].iterrows():
folium.Marker(location=[r['lat'], r['long']], popup=r['station']).add_to(map_osm)
map_osm
folium.Mapでオブジェクトを作成。locationで中心の座標、zoom_startでズームを指定しています。Markerで各マーカーの座標とポップアップで表示させる文字を指定し、add_toで先に作成したMapオブジェクトに追加します。
全部で272のステーションがあるのですが、全てを描かせようとすると途中で止まってしまったため、今回は利用回数の多い順、100件を表示させています。
出力
インタラクティブ地図が表示されました。マーカーをクリックするとステーション名が表示されます。サンフランシスコ市内に多くのステーションがありますが、対岸のオークランド、それにサンノゼにもあるようです。(前述のとおり、利用回数が多い100件のみの表示であることに注意)
・地図の保存
map_osm.save('index.html')
できた地図はsaveでhtml形式で保存ができます。
・マーカーの色を変更
map_osm = folium.Map(location=[37.795392,-122.394203],zoom_start=14)
k=1
for i, r in stations_startcount[0:100].iterrows():
if k<=10:
icon_color="red"
else:
icon_color="green"
folium.Marker(
location=[r['lat'], r['long']],
popup=r['station'],
icon=folium.Icon(color=icon_color)
).add_to(map_osm)
k=k+1
map_osm
利用の多い10件を赤色で、そのほかを緑のマーカーで表示させてみました。folium.Iconで色が変更できます。マーカーに「i」の表示が出てしまいましたが、最初の地図と同じマーカーで色を変える方法が分かりませんでした。
・円の大きさで表示
map_osm = folium.Map(location=[37.795392,-122.394203],zoom_start=12)
for i, r in stations_startcount[0:100].iterrows():
folium.Circle(
location=[r['lat'], r['long']],
radius=r["start_count"] / 20,
popup=r['station'],
color='#3186cc',
fill_color='#3186cc',
fill=True,
).add_to(map_osm)
map_osm
最後に利用回数を円の大きさで表します。folium.Circleで円の表示、radiusで円の半径を指定します。利用回数は最大約13000回で、この数字では大きすぎて円が表示されなかったので、20で割っています。
color、fill_colorで色の指定、fillでTrueにしないと塗りつぶしされないのでご注意を(デフォルトでは塗りつぶし無し)
出力
サンフランシスコ市内の数ヶ所の利用回数が非常に大きくなっています。オークランド、サンノゼでの利用はサンフランシスコと比べて少ないようです。
foliumでは比較的簡単にインタラクティブ地図が作れて楽しいです。
参考:
folium quickstart
https://python-visualization.github.io/folium/quickstart.html
け日記 Python: foliumで地図を描画する
https://ohke.hateblo.jp/entry/2018/03/02/230000
Qiita: Folium: Pythonでデータを地図上に可視化
https://qiita.com/nanakenashi/items/824c0cb16860ca59a424
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
プログラミング学習:edx UCバークレー データサイエンス基礎講座の紹介(python)
https://eneprog.blogspot.com/2018/04/edx-uc-python.html
サンフランシスコのレンタサイクルのデータを見てみる(基本統計量、python、pandas、ヒストグラム)
https://eneprog.blogspot.com/2018/05/pythonpandas_17.html
サンフランシスコのレンタサイクルのデータを見てみる その2。よく使われているステーションを調べる(python、pandas、groupby、pivot_table)
https://eneprog.blogspot.com/2018/05/pythonpandasgroupbypivottable.html
プログラミング学習:pandasでweb上の表を取得する。(python)
https://eneprog.blogspot.com/2018/04/pandaswebpython.html プログラミング学習:各国の女性の平均就学期間をグラフ化する。(python,pandas)
https://eneprog.blogspot.com/2018/05/pythonpandas.html