I have a dataframe where I would like to pivot my data to fit a specific format, making sure the dates are consecutive.
Data
ID Q122_c_en Q122con_s Q222_c_en Q222con_s Q322_c_en Q322con_s Q422_c_en Q422con_s
AA 900 89 1000 90 1200 92 1000 90
BB 1000 10 1000 20 1100 25 1300 30
Desired
ID Date con_en con_s
AA Q122 900 89
AA Q222 1000 90
AA Q322 1200 92
AA Q422 1000 90
BB Q122 1000 10
BB Q222 1000 20
BB Q322 1100 25
BB Q422 1300 30
Doing
df.pivot(index="ID", columns="Date", values=["con_en", "con_s"])
I am using pivot, however, the format does not reflect the desired above format. Any suggestion is appreciated.
source https://stackoverflow.com/questions/72650613/pivot-values-in-specific-order
Comments
Post a Comment