尝试:
x = pd.wide_to_long(
df,
stubnames="performance",
sep="_",
i=["id", "age", "Gender"],
j="Performance_Window",
)[["performance"]].reset_index(level=3)
y = pd.wide_to_long(
df,
stubnames="snapshot",
sep="_",
i=["id", "age", "Gender"],
j="Snapshot_Window",
)[["snapshot"]].reset_index(level=3)
print(
pd.concat([x, y], axis=1)
.reset_index()
.rename(columns={"performance": "Performance_Value", "snapshot": "Snapshot_Value"})
)
输出:
id age Gender Performance_Window Performance_Value Snapshot_Window Snapshot_Value
0 1 34 M 13 1
1 1 34 M 17 30 5 80
2 1 34 M 19 30 7 40
3 2 42 F 13 55 1 65
4 2 42 F 17 15 5 60
5 2 42 F 19 45 7 25