0

I have a dataframe-

df=pd.DataFrame({'city':['a','a','a','b','b','b'],'V':[1,2,3,1,2,3],'O':[33,44,55,66,77,88]})

Now I wanted to create a new dataframe which has unique elements of column V along with the corresponding values from the 'O' column. Output-

   city   1   2   3
0   a    33  44  55
1   b    66  77  88
ubuntu_noob
  • 2,305
  • 6
  • 23
  • 64

1 Answers1

0

You can use pivot_table for this:

df = df.pivot_table('O', ['city'], 'V')
print(df)

V      1   2   3
city
a     33  44  55
b     66  77  88
NYC Coder
  • 7,424
  • 2
  • 11
  • 24