chloe4
chloe4

Reputation: 23

Rearrange a dataFrame in python

I am on a code in python and I would like to transform this dataFrame :

Before:

enter image description here

Into this DataFrame :

enter image description here

I tried to pivot the table with the command :

pd.pivot_table(mytable, index = [JAN, FEB, MAR])

But I am not satisfied by the result...

Upvotes: 1

Views: 69

Answers (1)

zipa
zipa

Reputation: 27899

This should work:

df = pd.melt(df, id_vars=['Word'], value_vars=['JAN', 'FEB', 'MAR']).dropna()
df = df.pivot(index='variable', columns='value', values='Word')

Upvotes: 2

Related Questions