gpt4 book ai didi

python - Pandas:使用 groupie 的平行图

转载 作者:太空宇宙 更新时间:2023-11-03 13:56:59 26 4
gpt4 key购买 nike

我想知道是否有人可以帮助我进行平行坐标绘图。

首先,数据如下:

Data它的数据来自: https://data.cityofnewyork.us/Transportation/2016-Yellow-Taxi-Trip-Data/k67s-dv2t

因此,我尝试标准化一些特征,并用它来计算一周中每一天的行程距离、乘客数量和付款金额的平均值。

from pandas.tools.plotting import parallel_coordinates

feature = ['trip_distance','passenger_count','payment_amount']

#normalizing data
for feature in features:
df[feature] = (df[feature]-df[feature].min())/(df[feature].max()-df[feature].min())

#change format to datetime
pickup_time = pd.to_datetime(df['pickup_datetime'], format ='%d/%m/%y %H:%M')
#fill dayofweek column with 0~6 0:Monday and 6:Sunday
df['dayofweek'] = pickup_time.dt.weekday

mean_trip = df.groupby('dayofweek').trip_distance.mean()
mean_passanger = df.groupby('dayofweek').passenger_count.mean()
mean_payment = df.groupby('dayofweek').payment_amount.mean()

#parallel_coordinates('notsurewattoput')

所以如果我打印mean_trip:

enter image description here

它显示了一周中每一天的平均值,但我不确定如何使用它来绘制一个平行坐标图,所有 3 个平均值都在同一个图上。

有人知道如何实现吗?

最佳答案

我认为你可以将 3 次聚合平均值更改为输出 DataFrame 而不是 3 系列:

mean_trip = df.groupby('dayofweek').trip_distance.mean()
mean_passanger = df.groupby('dayofweek').passenger_count.mean()
mean_payment = df.groupby('dayofweek').payment_amount.mean()

至:

from pandas.tools.plotting import parallel_coordinates

cols = ['trip_distance','passenger_count','payment_amount']
df1 = df.groupby('dayofweek', as_index=False)[cols].mean()
#https://stackoverflow.com/a/45082022
parallel_coordinates(df1, class_column='dayofweek', cols=cols)

关于python - Pandas:使用 groupie 的平行图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49532995/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com