Progeny
Progeny

Reputation: 692

Pandas from matrix to DataFrame

I've a matrix, user-item matrix that I generate from:

matrix = df.pivot(index='user', columns='item', values='rating')

each row of matrix correspond to a user, each column to an item. Ordered list of users and items are stored in two lists so that i-th element of users list correspond to the id of i-th user in the i-th matrix's row.

After some processing on that matrix I want to switch back into a DataFrame with tree columns (user, item, rating)

   user  item  rating
0     1  1907     4.0
1     1  1028     5.0
2     1   608     4.0
3     1  2692     4.0
4     1  1193     5.0

I can't figure out how. Suggestions?

Upvotes: 0

Views: 97

Answers (2)

Scott Boston
Scott Boston

Reputation: 153460

You can use melt:

df_tab = df.reset_index().melt(id_vars='user',value_name='ratings')

Output:

   user  item  ratings
0     1   608      4.0
1     1  1028      5.0
2     1  1193      5.0
3     1  1907      4.0
4     1  2692      4.0

and back to matrix:

matrix = df_tab.pivot(index='user', columns='item', values='ratings')

Output:

item  1028  1193  1907  2692  608
user                             
1      5.0   5.0   4.0   4.0  4.0

Upvotes: 0

cs95
cs95

Reputation: 402263

Given a pivoted dataframe of this format -

item  608   1028  1193  1907  2692
user                              
1      4.0   5.0   5.0   4.0   4.0

You can stack your data and then reset the index -

df.stack().reset_index(name='rating')

   user  item  rating
0     1   608     4.0
1     1  1028     5.0
2     1  1193     5.0
3     1  1907     4.0
4     1  2692     4.0

Upvotes: 1

Related Questions