0

I have a dataset in the following format:

Keyword      Type       Count
"Key 1"      Normal     5
"Key 1"      Sponsored  2
"Key 2"      Normal     1
"Key 2"      Sponsored  0
"Key 3"      Normal     5
"Key 4"      Sponsored  3
"Key 5"      Normal     5
"Key 5"      Sponsored  4

I would like to change the schema of the table in something like this:

Keyword      Normal     Sponsored
"Key 1"      5          2
"Key 2"      1          0
"Key 3"      5          N/A
"Key 4"      N/A        3
"Key 5"      5          4

I have no idea at the moment about how to do it (except for something like iterating every keyword, and rebuilding the DF from scratch... I believe there is a better way?)

Federico Dorato
  • 710
  • 9
  • 27
  • df.pivot(index='Keyword', columns='Type', values='Count') should do the trick. See https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.pivot.html#pandas.DataFrame.pivot for details. – the_man_in_black Oct 19 '20 at 14:13
  • And I will go through the duplicate question properly ;) Thank you very much @JonasMickel – Federico Dorato Oct 19 '20 at 14:14

0 Answers0