Datascience
Datascience

Reputation: 45

Reshape the dataframe in python

I have a Music dataset. Where two columns ('Music_id' and 'Tag_name' ) are there. Sample data:

Music_id       Tag_name
  1004         Base Guitar
  1004         Guitar
  2004         Base guitar
  1005         Keyboard
  1003         Keyboard
  1004         Keyboard

I want to reshape this dataframe where all tag_name should become the column name, so that every row should have unique Music_id.

I read about the concept of stratification and merging but not sure how to apply in this situation. Any help?

Upvotes: 2

Views: 82

Answers (1)

wwnde
wwnde

Reputation: 26676

Question not clear. Maybe give us what you expect output to be

I guess you need this?

pd.crosstab(df.Music_id, df.Tag_name, dropna=False)

enter image description here

Upvotes: 2

Related Questions