gpt4 book ai didi

python - 按几个属性分组的 Pandas 直方图

转载 作者:行者123 更新时间:2023-12-04 10:16:53 24 4
gpt4 key购买 nike

我有一个数据框,其中包含每个站点、每次巴士旅行和每天的乘客数量。

我想绘制一个直方图,向我们显示 [trip_id, day] 的所有不同组合的乘客数量。按出发时间排序。

下面的最小示例产生了预期的结果:

import pandas as pd
import random

# Dummy dataframe where:
# day = day of operation
# line = bus line number
# trip = the trip ID
# dep_time = departure time
# stop_name = the stop name
# load = number of passenger at each stop
d = {'day': ['Fri'] * 6 + ['Sat'] * 6 + ['Fri'] * 6 + ['Sat'] * 6,
'line': [1] * 12 + [2] * 12,
'trip': [1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8],
'dep_time': list(range(1, 7)) * 4,
'stop_name': ['George Street', 'Casino', 'Beauregard'] * 4 + ['Virginia Street', 'Monbenont', 'Baker street'] * 4,
'load': [random.randint(1, 10) for x in range(24)]}
df = pd.DataFrame(data=d)

# Get the unique day and trip ID
uday = df['day'].unique().tolist()
utrip = df['trip'].unique().tolist()

# For each group of distinct [day,trip] plot an histogram of the number of passenger at each stop
# and sort the stop by departure time.
for day in uday:
for trip in utrip:
# Filter the dataframe for each unique day, trip ID and direction.
df_to_plot = df.sort_values('dep_time')[(df['day'] == day) & (df['trip'] == trip)]
if not df_to_plot.empty:
title = 'line: ' + str(df_to_plot['line'].unique()[0]) \
+ ', ' \
+ 'trip_id: ' + str(trip) \
+ ' ' \
+ day

ax = df_to_plot.plot.bar(x='stop_name', y='load', rot=90, title=title)

此代码生成 8 个直方图,但我必须为每个组创建一个循环。有没有办法通过使用某种 group_by 来产生相同的结果?与 Pandas 一起工作?

最佳答案

IIUC,是的,这可以通过 groupby 来完成:

for (d,t), v in df.sort_values('dep_time').groupby(['day','trip']):
# your other plot commands here:
if len(v):
v.plot.bar(x='stop_name',y='load')

关于python - 按几个属性分组的 Pandas 直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61028382/

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