Xorbits 是探索和分析大型数据集的理想工具。本文将用数据领域经典的数据集,纽约出租车数据,来展示 Xorbits 的能力。该数据集记录了 2009 年至 2022 年纽约出租车行程记录。我们将展示如何使用 Xorbits 对纽约出租车数据进行一些初始的探索。
您也可以选择用 Google Colab 或 Kaggle 并直接运行本案例。
案例的原始代码以及更多其他案例,请见 Xorbits Examples。
安装 Xorbits 及相关依赖
# 安装依赖
pip install xorbits>=0.1.2 plotly==5.11.0 pyarrow下载数据集
我们将会使用下列两个数据集:
下载纽约出租车区域信息(区域划分与相关地理信息):
wget https://d37ci6vzurychx.cloudfront.net/misc/taxi+_zone_lookup.csv
wget https://data.cityofnewyork.us/api/geospatial/d3c5-ddgc\?method\=export\&format\=GeoJSON -O taxi_zones.geojson下载 2021 年纽约出租车行驶记录:
for i in {1..12}
do
wget https://d37ci6vzurychx.cloudfront.net/trip-data/yellow_tripdata_2021-$(printf "%02d" $i).parquet
done
mkdir yellow_tripdata_2021
mv yellow_tripdata_2021-*.parquet yellow_tripdata_2021初始化 Xorbits
首先,初始化 Xorbits:
import xorbits
# Initialize Xorbits in the local environment.
xorbits.init()数据加载
第二步,将数据加载到 Xorbits DataFrame 中。我们可以使用 read_parquet() 函数来完成,该函数允许我们指定 Parquet 文件的位置和在读取数据时要使用的其他选项,如密钥等。
加载纽约出租车行驶记录并清理错误数据:
import datetime
import xorbits.pandas as pd
trips = pd.read_parquet(
'yellow_tripdata_2021',
columns=[
'tpep_pickup_datetime',
'tpep_dropoff_datetime',
'trip_distance',
'PULocationID',
'DOLocationID',
])
# Remove outliers.
trips = trips[(trips[&#39;tpep_pickup_datetime&#39;] >= datetime.datetime(2021, 1, 1)) & (trips[&#39;tpep_pickup_datetime&#39;] <= datetime.datetime(2021, 12, 31))]加载后的数据如下:
tpep_pickup_datetime | tpep_dropoff_datetime | trip_distance | PULocationID | DOLocationID | 0 | 2021-01-01 00:30:10 | 2021-01-01 00:36:12 | 2.10 | 142 | 43 | 1 | 2021-01-01 00:51:20 | 2021-01-01 00:52:19 | 0.20 | 238 | 151 | ... | ... | ... | ... | ... | ... | 3212646 | 2021-12-30 23:28:00 | 2021-12-30 23:43:00 | 4.46 | 246 | 87 | 3212672 | 2021-12-31 00:00:00 | 2021-12-31 00:08:00 | 1.02 | 163 | 229 | 加载纽约出租车区域数据:
taxi_zones = pd.read_csv(&#39;taxi+_zone_lookup.csv&#39;, usecols=[&#39;LocationID&#39;, &#39;Zone&#39;])
taxi_zones.set_index([&#39;LocationID&#39;], inplace=True)加载后的数据如下:
LocationID | Zone | 1 | Newark AIrport | 2 | Jamaica Bay | ... | ... | 264 | NV | 265 | NaN | 最后加载出租车区域的地理信息:
import json
with open(&#39;taxi_zones.geojson&#39;) as fd:
geojson = json.load(fd)初始探索
将数据加载到 DataFrame 后,我们可能希望通过查看行数和列数、每列的数据类型以及数据的前几行来了解数据的整体结构。 我们可以分别使用shape、dtypes和head()来实现:
trips.shape
(30833535, 5)
trips.dtypes
tpep_pickup_datetime datetime64[ns]
tpep_dropoff_datetime datetime64[ns]
trip_distance float64
PULocationID int64
DOLocationID int64
dtype: object
trips.head()tpep_pickup_datetime | tpep_dropoff_datetime | trip_distance | PULocationID | DOLocationID | 0 | 2021-01-01 00:30:10 | 2021-01-01 00:36:12 | 2.10 | 142 | 43 | 1 | 2021-01-01 00:51:20 | 2021-01-01 00:52:19 | 0.20 | 238 | 151 | 2 | 2021-01-01 00:43:30 | 2021-01-01 01:11:06 | 14.70 | 132 | 165 | 3 | 2021-01-01 00:15:48 | 2021-01-01 00:31:01 | 10.60 | 138 | 132 | 4 | 2021-01-01 00:31:49 | 2021-01-01 00:48:21 | 4.94 | 68 | 33 | 时序分析
分析纽约出租车数据集的一个角度是查看乘车数量如何随时间变化。一个特别有趣的分析是找出每个工作日每小时的乘车数量。 我们可以在 DataFrame 中创建两个额外的列,分别表示行程开始的星期和小时。然后,我们可以使用 groupby 方法按照星期几和小时分组数据,并计算每组的旅途数量。
trips[&#39;PU_dayofweek&#39;] = trips[&#39;tpep_pickup_datetime&#39;].dt.dayofweek
trips[&#39;PU_hour&#39;] = trips[&#39;tpep_pickup_datetime&#39;].dt.hour
gb_time = trips.groupby(by=[&#39;PU_dayofweek&#39;, &#39;PU_hour&#39;], as_index=False).agg(count=(&#39;PU_dayofweek&#39;, &#39;count&#39;))PU_dayofweek | PU_hour | count | 0 | 0 | 0 | 55630 | 1 | 0 | 1 | 28733 | ... | ... | ... | ... | 166 | 6 | 22 | 123082 | 167 | 6 | 23 | 90842 | 然后,我们可以使用像 Plotly 这样的库来可视化时间序列数据。下面的图表显示了每小时的乘车次数。从图表中可以看出,人们更倾向于在下午出行。此外,在周末,人们通常倾向于晚归。
import plotly.express as px
b = px.bar(
gb_time.to_pandas(),
x=&#39;PU_hour&#39;,
y=&#39;count&#39;,
color=&#39;PU_dayofweek&#39;,
color_continuous_scale=&#39;sunset_r&#39;,
)
b.show()
每小时的乘车次数
下面的图表显示了一周中每天的乘车次数。
b = px.bar(
gb_time.to_pandas(),
x=&#39;PU_dayofweek&#39;,
y=&#39;count&#39;,
color=&#39;PU_hour&#39;,
color_continuous_scale=&#39;sunset_r&#39;,
)
b.show()
一周中每天的乘车次数
空间分析
分析纽约出租车数据集的另一个角度是查看旅途的空间分布模式。我们可以使用groupby方法按旅途起点区域 ID 和下车地点区域 ID 将数据分组,计算每组的旅途数量,并可视化:
gb_pu_location = trips.groupby([&#39;PULocationID&#39;], as_index=False).agg(count=(&#39;PULocationID&#39;, &#39;count&#39;))
gb_pu_location = gb_pu_location.to_pandas()
import plotly.graph_objects as go
fig = go.Figure(
go.Choroplethmapbox(
geojson=geojson,
featureidkey=&#39;properties.location_id&#39;,
locations=gb_pu_location[&#39;PULocationID&#39;],
z=gb_pu_location[&#39;count&#39;],
colorscale=&#34;Viridis&#34;,
marker_opacity=0.7,
marker_line_width=0.1
)
)
fig.update_layout(
mapbox_style=&#34;carto-positron&#34;,
mapbox_zoom=9,
mapbox_center = {&#34;lat&#34;: 40.7158, &#34;lon&#34;: -73.9805},
height=600,
)
fig.show()
旅途起点的空间分布
我们还可以按下车地点区域 ID 对数据进行分组,并可视化:
gb_do_location = trips.groupby([&#39;DOLocationID&#39;], as_index=False).agg(count=(&#39;DOLocationID&#39;, &#39;count&#39;))
gb_do_location = gb_do_location.to_pandas()
fig = go.Figure(
go.Choroplethmapbox(
geojson=geojson,
featureidkey=&#39;properties.location_id&#39;,
locations=gb_do_location[&#39;DOLocationID&#39;],
z=gb_do_location[&#39;count&#39;],
colorscale=&#34;Viridis&#34;,
marker_opacity=0.7,
marker_line_width=0.1
)
)
fig.update_layout(
mapbox_style=&#34;carto-positron&#34;,
mapbox_zoom=9,
mapbox_center = {&#34;lat&#34;: 40.7158, &#34;lon&#34;: -73.9805},
height=600,
)
fig.show()
下车地点的空间分布
我们还可以探索出租车区域之间的交通情况:
gb_pu_do_location = trips.groupby([&#39;PULocationID&#39;, &#39;DOLocationID&#39;], as_index=False).agg(count=(&#39;PULocationID&#39;, &#39;count&#39;))
# Add zone names.
gb_pu_do_location = gb_pu_do_location.merge(taxi_zones, left_on=&#39;PULocationID&#39;, right_index=True)
gb_pu_do_location.rename(columns={&#39;Zone&#39;: &#39;PUZone&#39;}, inplace=True)
gb_pu_do_location = gb_pu_do_location.merge(taxi_zones, left_on=&#39;DOLocationID&#39;, right_index=True)
gb_pu_do_location.rename(columns={&#39;Zone&#39;: &#39;DOZone&#39;}, inplace=True)
gb_pu_do_location.sort_values([&#39;count&#39;], inplace=True, ascending=False)PULocationID | DOLocationID | count | PUZone | DOZone | 44108 | 237 | 236 | 225931 | Upper East Side South | Upper East Side North | 43855 | 236 | 237 | 194655 | Upper East Side North | Upper East Side South | ... | ... | ... | ... | ... | ... | 868 | 9 | 29 | 1 | Auburndale | Brighton Beach | 49337 | 264 | 105 | 1 | NV | Governor&#39;s Island/Ellis Island/Liberty Island | 总结
正如我们在本文中使用纽约市出租车数据集所展示的,Xorbits 是一种极其强大的用于探索和分析大型数据集的工具。 通过按照本文中所述的步骤,您可以更好地了解 Xorbits 的功能、易用性,以及如何将其与其他 Python 库集成,以简化您的数据分析工作流程。
如果你对 Xorbits 感兴趣,那么事不宜迟,快来试用吧!
资料
- 官方网站:https://xorbits.cn
- Github:https://github.com/xprobe-inc/xorbits
- Gitee:https://gitee.com/xprobe-inc/xorbits
- 文档:https://doc.xorbits.cn
- 社区:https://xorbits.cn/community
上一篇:喜大普奔!春节成纽约市法定假日! 下一篇:盘点纽约的安全“绿灯区” |