Yaosong Yu
Yaosong Yu

Reputation: 1

pandas create paired data from a dataframe

I have a pd.DataFrame that like this

    User  M&Ms  Skittles  Snickers  Laffy Taffy  Caramel Chew
0   Lara     3         0         0            0             0
1  Daisy     4         0         1            1             0
2   Alyx     0         0         0            0             5
3  Sarah     0         3         0            0             0

I want to create a dataframe that looks like this

    user          item  rating
0   Lara          m_ms       3
1   Lara      snickers       5
2  Daisy          m_ms       4
3  Daisy   laffy_taffy       1
4   Alyx  caramel_chew       5
5  Sarah      skittles       3
6  Sarah      snickers       2

Is there anyway to do that? Thanks

Upvotes: 0

Views: 84

Answers (2)

rafaelc
rafaelc

Reputation: 59274

Use melt + query

df.melt('User').query('value > 0')

     User      variable  value
0    Lara          M&Ms      3
1   Daisy          M&Ms      4
7   Sarah      Skittles      3
9   Daisy      Snickers      1
13  Daisy   Laffy Taffy      1
18   Alyx  Caramel Chew      5

Upvotes: 2

Niels Henkens
Niels Henkens

Reputation: 2696

You can use df.stack()

# First set the index to user
df.set_index(‘user’, inplace =True)
# then stack
df_new = df.stack()
# reset index
df_new.reset_index(inplace=True)

Using pd.melt() is another option.

Upvotes: 0

Related Questions