1

I have the dataframe:

userId  |    0  |     1 | ..... |     N
----------------------------------------
1          2.3      2.5             1.7
2          4.5      3.1             1.5
3          1.8      4.5             2.6
4          3.4      4.2             2.8
5          1.7      2.4             2.9
...

how can I unpivot this to get a dataframe of the form:

userId  |  itemId   | rating
1            1          2.3
1            2          2.5
...
1            N          1.7
2            1          4.5
2            2          3.1
...
2            N          1.5
3            1          1.8
...

EDIT:

Here is some pastable code:

arr = np.array([[2.3, 2.5, 1.7],
                [4.5, 3.1, 1.5],
                [3.4, 4.2, 2.8]])

test_df = pd.DataFrame(arr)
test_df['userId'] = list(range(1, test_df.shape[0]+1))
Qubix
  • 4,161
  • 7
  • 36
  • 73
  • Would be much easier if you had provided copy-pastable data... setting up the df takes longer than coding the solution. – timgeb May 29 '20 at 11:43

0 Answers0