1

I have a data frame which I get after doing group by which looks like below:-

data = [[1,'US', 10], [1,'CA', 15], [1,'IN', 14],
        [2,'US', 15], [2,'CA', 9], [2,'IN', 1],
        [3,'US', 16], [3,'CA', 8], [3,'IN', 33]]
 
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['fan', 'country','value'])
 
# print data frame.
df

enter image description here

which I want to convert into:-

enter image description here

abhi
  • 389
  • 2
  • 13

1 Answers1

2

You just need pivot:

>>> df.pivot(index="country", columns="fan", values="value").reset_index()
fan country   1   2   3
0        CA  15   9   8
1        IN  14   1  33
2        US  10  15  16
not_speshal
  • 22,093
  • 2
  • 15
  • 30