我有一个多索引的DataFrame,该索引由(阶段、service_group、站点、年份、期间)组成,其目的是在指定多索引的所有5个值时返回"capacity_required“。例如,在最后阶段,服务组西,弥尔顿站,2025年,周期高峰时刻1,required_capacity是1500。
目前有7个可能的时段,其中两个是“非高峰时间”和“肩时”。
我需要在多个索引的每一个实例中添加一个新的周期,称为Off峰值肩,其中新的值被定义为非高峰时间和肩扛小时的平均值。
到目前为止,我有以下代码:
import pandas as pd
import os
directory = '/Users/mark/PycharmProjects/psrpcl_data'
capacity_required_file = 'Capacity_Requirements.csv'
capacity_required_path = os.path.join(directory, capacity_required_file)
df_capacity_required = pd.read_csv(capacity_required_path, sep=',',
usecols=['phase', 'service_group', 'station', 'year', 'period', 'capacity_required'])
df_capacity_required.set_index(['phase', 'service_group', 'station', 'year'], inplace=True)
df_capacity_required.sort_index(inplace=True)
print(df_capacity_required.head(14))上述代码的输出是:
period capacity_required
phase service_group station year
Early Barrie Allandale Waterfront Station 2025 AM Peak Period 490
2025 Off-Peak Hour 100
2025 PM Peak Period 520
2025 Peak Hour 2 250
2025 Peak Hour 5 180
2025 Peak Hour 6 180
2025 Shoulder Hour 250
2026 AM Peak Period 520
2026 Off-Peak Hour 50
2026 PM Peak Period 520
2026 Peak Hour 2 260
2026 Peak Hour 5 180
2026 Peak Hour 6 180
2026 Shoulder Hour 250以上仅仅是大约30K线路中的前14行。这表明你有两年的时间。注意,每年有7个周期。
我试图创造一个新的“时期”,称为“非峰肩”,以添加到每一个单一的(阶段,service_group,车站,年)组合,这是平均的非峰值和肩膀。
以下一行正确地计算了每个索引值的一个非峰值肩值:
off_peak_shoulder = df_capacity_required.loc[df_capacity_required.period == 'Off-Peak Hour', 'capacity_required'].add(
df_capacity_required.loc[df_capacity_required.period == 'Shoulder', 'capacity_required']).div(2)
print(off_peak_shoulder)以上代码提供了以下(正确的)非峰值肩部序列作为输出:
phase service_group station year
Early Barrie Allandale Waterfront Station 2025 0.0
2026 0.0
2027 0.0
2028 0.0
2029 0.0
...
Initial Union Pearson Express Pearson Station 2023 160.0
2024 160.0
Weston Station 2022 80.0
2023 105.0
2024 105.0问题:如何将off_peak_shoulder系列合并/加入到df_capacity_required中,以便在“期间”下再添加一个条目,如下所示?
period capacity_required
phase service_group station year
Early Barrie Allandale Waterfront Station 2025 AM Peak Period 490
2025 Off-Peak Hour 100
2025 PM Peak Period 520
2025 Peak Hour 2 250
2025 Peak Hour 5 180
2025 Peak Hour 6 180
2025 Shoulder Hour 250
2025 Off-Peak Shoulder 175
2026 AM Peak Period 520
2026 Off-Peak Hour 50
2026 PM Peak Period 520
2026 Peak Hour 2 260
2026 Peak Hour 5 180
2026 Peak Hour 6 180
2026 Shoulder Hour 250
2025 Off-Peak Shoulder 150发布于 2020-09-25 15:13:46
我在这个问题上睡了一觉,醒来时想到了一个解决办法。我已经有了我需要的值列表,并为每个值设置了正确的多索引。我想我需要一些复杂的多索引插入代码,但实际上,我只需要将创建的DataFrame以与原始DataFrame相同的形式放置,并将两者连接在一起。
这是我添加的代码。注第一行代码与原始代码相同,但我添加了对reset_index的调用。
df_new = df_capacity_required.loc[df_capacity_required.period == 'Off-Peak Hour', 'capacity_required'].add(
df_capacity_required.loc[df_capacity_required.period == 'Shoulder Hour', 'capacity_required']).div(2).reset_index()
df_new['period'] = 'Off-Peak Shoulder'
df_new.set_index(['phase', 'service_group', 'station', 'year'], inplace=True)
df_capacity_required = concat([df_capacity_required, df_new])
df_capacity_required.sort_index(inplace=True)
print_full(df_capacity_required.head(16))而该print语句提供了以下所需的输出:
period capacity_required
phase service_group station year
Early Barrie Allandale Waterfront Station 2025 AM Peak Period 490
2025 Off-Peak Hour 100
2025 PM Peak Period 520
2025 Peak Hour 2 250
2025 Peak Hour 5 180
2025 Peak Hour 6 180
2025 Shoulder Hour 250
2025 Off-Peak Shoulder 175
2026 AM Peak Period 520
2026 Off-Peak Hour 50
2026 PM Peak Period 520
2026 Peak Hour 2 260
2026 Peak Hour 5 180
2026 Peak Hour 6 180
2026 Shoulder Hour 250
2026 Off-Peak Shoulder 150但是感谢所有读到这个问题的人。很高兴知道在StackOverflow上有很多人愿意帮助被困的人。
https://stackoverflow.com/questions/64058645
复制相似问题