1

I have a df that contains two rows and I wanted to use the first row as a header in a new df.

This is what my data looks like:

ver time
a 2.31
b 3.45
b 3.75
a 2.21
b 3.87
b 4.02
a 1.97
a 3.56

This is what I am trying to get:

a b
2.31 3.45
2.21 3.75
1.97 3.87
3.56 4.02
Riley
  • 1,729
  • 1
  • 5
  • 13
sd_5_1_1
  • 23
  • 4
  • These sorts of questions are not ones that should be submitted to stackoverflow. You will find your answer here. https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.pivot.html – Riley Oct 24 '21 at 02:04
  • Q/A #10 in the duplicate. – Henry Ecker Oct 24 '21 at 02:27

1 Answers1

2

Try with cumcount create the key then pivot

out = df.assign(key=df.groupby('ver').cumcount()).pivot('key','ver','time')
ver     a     b
key            
0    2.31  3.45
1    2.21  3.75
2    1.97  3.87
3    3.56  4.02
BENY
  • 296,997
  • 19
  • 147
  • 204