如何将系列合并为DataFrame的多索引的一列的成员

问题描述

我有一个包含由(阶段,service_group,站,年份,周期)组成的多索引的DataFrame,其目的是在指定了所有5个多索引值时返回“ capacity_required”。 例如,在Final组,West服务组,Milton站,2025年,高峰时段1期间,required_capacity为1500。

当前有7个可能的时间段,其中两个是“非高峰时间”和“肩负时间”。

我需要为多索引的每个实例添加一个新的期间,称为“非高峰期肩”,其中新值定义为“非高峰时数”和“非高峰期”的平均值。

到目前为止,我有以下代码

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','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

以上只是大约3万行的前14行。这向您显示了两年的期限。请注意,每年有7个期间。

我正在尝试创建一个新的“期间”,称为“非高峰肩峰”,将其添加到每个单独的(阶段,service_group,站,年份)组合中,这将成为非高峰和肩峰的平均值。 / p>

下面的行正确地为每个索引值计算一个“峰顶”值:

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以使“ Off-Peak Shoulder”成为“期间”下的另一个条目,如下所示?

                                                               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

解决方法

我睡着了,然后醒了一个解决方案。我已经有了所需的值列表,并为每个值设置了正确的多索引。我当时以为我需要一些复杂的多索引插入代码,但是实际上我只需要将创建的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))

该打印语句将提供以下所需输出:

                                                               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上有人愿意帮助某人陷入困境。