Reputation: 33
I have a dataframe which I pivot and I need to understand how I can reverse the order of the dates, so the most recent date is first.
E.g. Title Date Close
0 ABBA. 29/01/2021 164.34
1 ABBA. 28/01/2021 154.34
2 ABBA. 27/01/2021 144.34
3 ABBA. 26/01/2021 134.34
4 ABBA. 25/01/2021 124.34
5 ABBA. 24/01/2021 114.34
When I pivot it ptable = dff.pivot_table(index=['Title'], columns='Date', values='Close')
I get :
Title 24/01/2021 25/01/2021 26/01/2021
ABBA 114.34. 124.34. 134.34
What I want is:
Title 29/01/2021 28/01/2021 27/01/2021
ABBA 164.34. 154.34. 144.34
I can't seem to see how?
Upvotes: 0
Views: 649
Reputation: 1213
Since the data is correct you only need to reindex your columns in your desired order. Try this:
pcolumns = list(ptable.columns)
pcolumns.reverse()
ptable[pcolumns]
Alternatively, you use .sort_index()
.
ptable.sort_index(ascending=False, axis=1)
Upvotes: 1