0

I have a data frame that looks like this:

df=pd.DataFrame({'Machine':['A','A','B','B','B'],
                 'Date':['1/1/2020','4/5/2020','4/1/2020','4/3/2020','5/1/2020'],
                'Production':[5,9,44,13,34],
                })

  Machine      Date  Production
0       A  1/1/2020           5
1       A  4/5/2020           9
2       B  4/10/2020         44
3       B  4/3/2020          13
4       B  5/1/2020          34

And I want it to be converted to this:

    A   B
0   5  13
1   9  44
2      34

Where the index is simply the rank of the dates in order.

Is there a way to do it without creating the rank column first?

raw_data["RANK"] = raw_data.groupby('Machine')["Date"].rank(method="first", ascending=True)
new_data = raw_data.pivot(columns = 'Machine', values = 'Production', index = 'RANK')
Stix
  • 39
  • 6
  • Actually your method is pretty straightforward. Another variation is `cumcount`, mentioned as Q/A 10 in [this guide](https://stackoverflow.com/questions/47152691/how-to-pivot-a-dataframe). But it's essentially the same idea. – Quang Hoang Mar 17 '21 at 16:10
  • you can pass it directly to the index index = `raw_data.groupby('Machine')["Date"].rank(method="first", ascending=True)` but your current method is clean and readable. – Umar.H Mar 17 '21 at 16:10
  • That helps, thank you! – Stix Mar 18 '21 at 15:57

0 Answers0