重塑 Pandas 数据框并按 2 级列分组

问题描述 投票:0回答:1

我有一个具有扁平结构的数据框,具有如下所示的唯一行。 Original Df

我需要如下所示重塑它的形状。 Desired reshaped format

使用数据透视表和交换级别,我设法获得了更接近结果的结果,但它无意中对 level1 子列进行了排序。 incorrect output

data = {
    "Case_ID": ["1-1 Max Export", "1-1 Max Export", "1-2 Max Export", "1-2 Max Export", "1-3 Max Export", "1-3 Max Export"],
    "Item": ["3-Winding TX", "R1-SUT1", "3-Winding TX", "R1-SUT1", "3-Winding TX", "R1-SUT1"],
    "HV Current": [0.5, 0.1, 0.4, 0.1, 0.5, 0.1],
    "Total Power": [114.5, 2.2, 113.4, 2.2, 100.0, 1.8],
    "Tap Pos.": [15, 3, 1, 3, 20, 3]
}


df = pd.DataFrame(data)   # Original Dataframe Format with Flat Structure

item_order = list (df.columns[2:])  # The second Level columns must be in same order as per the original df

# Pivot the DataFrame
reshaped_df = df.pivot_table(index='Case_ID', 
                             columns='Item', 
                             values=list (df.columns[2:]), 
                             aggfunc='first')

# Swap level 0 and level 1 columns
reshaped_df.columns = reshaped_df.columns.swaplevel(0, 1)

# Without.sort_index(axis=1) the code doesn't work. 
# The Level 0 and Level 1 colums shallbe in the same order as per the original df
reshaped_df = reshaped_df.sort_index(axis=1)

reshaped_df

点击位置。子列必须是每个类别的最后一个 子列顺序应按照原始 df(即 HV 电流、总功率、分接头位置)。

  • a)我正在寻找修复上面的代码。

  • b)也有兴趣看到有另一种方法可以实现这一点 使用数据透视表。

python pandas group-by pivot-table reshape
1个回答
0
投票

代码

out = (df
       .pivot_table(index='Case_ID', columns='Item', aggfunc='first')
       .swaplevel(0, 1, axis=1)
       .sort_index(
           axis=1, 
           key=lambda x: pd.Categorical(
               x, 
               categories=df.get(x.name, df.columns[2:]).unique()
           )
       )
)
© www.soinside.com 2019 - 2024. All rights reserved.