Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas Sort Values By Date Doesn't Sort By Year

I have a large data set that is in this format

enter image description here

I’d like to order this data set by the "created_at" column, so I converted the "created_at" column to type datetime following this guide:
https://www.geeksforgeeks.org/how-to-sort-a-pandas-dataframe-by-date/

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

data = pd.read_csv(PATH_TO_CSV)

data['created_at'] = data['created_at'].str.split("+").str[0]
data['created_at'] = pd.to_datetime(data['created_at'],format="%Y-%m-%dT%H:%M:%S")

data.sort_values(by='created_at')

But it’s not sorting by year as expected. The values starting with 2012 should be at the top, but they aren’t

print(data)
print(type(data['created_at'][0]))

enter image description here

What am I missing?

>Solution :

As in the comments already stated. the sorted df needs to be assigned again. sort_values doesn’t work inplace by default.

data = data.sort_values(by='created_at')

# OR 

data.sort_values(by='created_at', inplace=True)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading