Rearranging Dataframe date

Rearranging Dataframe date



How do I rearrange dates after importing a csv file, such that the most recent date is at the bottom and the oldest date is at the top?



I have tried using reindex but it doesn't work.



hello Nukesor, the date for example will be..


Date Price
5-2-2017 15.24
4-2-2017 18.21
3-2-2017 19.11
2-2-2017 20.28
1-2-2017 17.00



Now i will like to put 5-2-2017 at the bottom



Thanks guys.






How exactly does the date in your dataframe look like? What type are your entries in the date row? You might just need to call df['Date'] =pd.to_datetime(df.Date) and then sort by this column.

– Nukesor
Sep 9 '18 at 11:52


df['Date'] =pd.to_datetime(df.Date)






Hi Nukesor i have re-edited the question. Please take a look and kindly give your feedback. Thanks

– Jonathan
Sep 9 '18 at 12:58




2 Answers
2



You can use pd.to_datetime and then sort_values:


pd.to_datetime


sort_values


df = df.assign(Date=pd.to_datetime(df['Date']))
.sort_values('Date')

print(df)

Date Price
4 2017-01-02 17.00
3 2017-02-02 20.28
2 2017-03-02 19.11
1 2017-04-02 18.21
0 2017-05-02 15.24



Try this:


df['Date'] = pd.to_datetime(df['Date'])
df.sort_values('Date', ascending = True, inplace=True)



First line converts date to comprehensible datetime format for the dataframe. Second line sorts the values in ascending order. Without first line order would be as a string, and that is not the behaviour expected.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế