I currently have the following dataframe:
1_1 1_2 1_3 1_4 1_5 2_1 ... 9_5 10_1 10_2 10_3 10_4 10_5
0 0.049400 0.063812 0.097736 -0.077222 0.112779 -0.201620 ... 0.138246 0.015369 -0.083559 -0.186949 0.158505 -0.046787
1 -0.169837 0.093606 0.043157 0.095289 -0.078525 -0.026500 ... -0.054344 0.008955 0.045036 0.198438 0.197416 -0.057831
2 -0.192915 0.001477 0.077699 …
I want to obtain something like this:
cat u i mouse
0 1 1 0.049400
1 1 1 -0.169837
2 1 1 -0.192915
0 1 2 0.063812
1 1 2 0.093606
2 1 2 0.001477
…
Essentially, the lines represent the value of the cat column, the number before the underscore represents the u column and the number after represents the i column. Lastly, the mouse column is the value for the combination of the previous factors.
However, the solution should work for any data in such format.
So far, I have this, considering I have access to the list of u (in this case 1,2,3,4,5,6,7,8,9,10) and i (1,2,3,4,5), but the solution should work for different lists and different numbers of lines as well.
u_seq_stub = [u + '_' for u in u_seq]
df = pd.wide_to_long(df, u_seq_stub, i='u', j='i').reset_index().rename(columns={'_':'u'})
This doesn't work however, and throws "KeyError: "None of [Index(['userid'], dtype='object')] are in the [columns]""... I've also consulted this and it doesn't seem that different from what I want but there must be something I am misunderstanding.
I appreciate in advance any help.