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
  • 2,153
  • 1
  • 6
  • 16
sd5
  • 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
  • 317,841
  • 20
  • 164
  • 234
  • Thank you! This was helpful in getting to what I needed and came in handy on another project. – sd5 Oct 26 '21 at 00:41
  • @sd_5_1_1 If this help , would you like accept the answer ? check mark at the left – BENY Oct 26 '21 at 00:57