使用数据透视表和交换级别,我设法获得了更接近结果的结果,但它无意中对 level1 子列进行了排序。
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)也有兴趣看到有另一种方法可以实现这一点 使用数据透视表。
代码
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()
)
)
)