I need to unpivot a pandas dataframe. I am using pd.melt() function for this. It is working as expected, now I need to add an additional column "column_number" in my output. Example below:
name age gender id
a 18 m 1
b 20 f 2
Current Output:
id variable value
1 name a
1 age 18
1 gender m
2 name b
2 age 20
2 gender f
Expected Output:
id column_number variable value
1 1 name a
1 2 age 18
1 3 gender m
2 1 name b
2 2 age 20
2 3 gender f
Since my dataframe structure can change, I will not know if I have 3 columns or more in future. How can I generate this column_number column in melt results?