I have a pandas Dataframe that looks like this:
pid | fid | FirstReasonName | FirstReasonValue | SecondReasonName | SecondReasonValue |
---|---|---|---|---|---|
1 | 'x' | 'a' | 3 | 'b' | 8.2 |
2 | 'y' | 'c' | 8 | 'd' | 7 |
Now I want to unpivot it on pid
and fid
so that it would become this:
pid | fid | Reason | Value |
---|---|---|---|
1 | 'x' | 'a' | 3 |
1 | 'x' | 'b' | 8.2 |
2 | 'y' | 'c' | 8 |
2 | 'y' | 'd' | 7 |
How do I do this?
You can use melt
and then pivot_table
:
id_cols = ["pid", "fid"]
df = df.melt(id_vars=id_cols, var_name="ReasonType", value_name="Value")
df[["reason", "type"]] = df["ReasonType"].str.extract(r"(\w+Reason)(Name|Value)")
df = (
df.pivot_table(
index=id_cols + ["reason"], columns="type", values="Value", aggfunc="first"
)
.reset_index()
.drop(columns="reason")
)
df.columns.name = None
pid fid Name Value
0 1 x a 3
1 1 x b 8.2
2 2 y c 8
3 2 y d 7.0