This is the follow-up post of this one
Please find the MCVE here
My data frame looks similar as the following one:
ID Date ColA1 ColB1 ColA2 ColB2 ColA3 ColB3
id1 date1 1 2 3 4 5 6
id2 date2 7 8 9 10 11 12
I want to split the columns ColA2
, ColB2
, ColA3
, ColB3
and merge them again to the dataset as rows (and still keeping the ID
and Date
)
Expected output:
ID Date ColA ColB
id1 date1 1 2
id1 date1 3 4
id1 date1 5 6
id2 date2 7 8
id2 date2 9 10
id2 date2 11 12
I tried both
pd.wide_to_long(df, stubnames = ['ColA', 'ColB'], i = ['ID', 'Date'], j = 'value').reset_index([0,1])
and
pd.wide_to_long(df.reset_index(), stubnames = ['ColA', 'ColB'], i = ['ID', 'Date'], j = 'value').reset_index(drop=True)
But still got the same error, saying that the id variables need to uniquely identify each row
.
I also already tried the method here with reset_index()
and I still got the same problem.
I guess this problem came from my specific dataset. But I don't know what's wrong with it and how to figure it out, in order to solve this issue. Do you have any suggestion?
Please let me know what I can do to solve this issue. Thank you!