0

Have a df with values

name    marks    subject

mark     50       math
mark     75       french
tom      25       english
tom      30       Art
luca     100      math
luca     100      art

How to make a transpose of a dataframe so it looks like this

name math art french english

mark  50        75  
tom        30         25
luca  100  100

tried:

df.T and df[['marks','subject']].T

but

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
crawler
  • 43
  • 7

1 Answers1

0

This is a pivot. First we need to normalize the subject column, then we pivot.

df['subject'] = df['subject'].str.lower()
df.pivot(index='name', columns='subject', values='marks')

See here for more info: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.pivot.html#pandas.DataFrame.pivot

Kyle Parsons
  • 1,475
  • 6
  • 14