0

The solutions I have found in a similar question are not working for me. I have a pandas DataFrame including mock sales data. I want to sort by date since they are currently out of order. I have tried converting to a datetime object. I also tried creating a Month and Day column and sorting by them but that did not work either. Date is in YYYY-MM-DD format

Here is my solution:

import pandas as pd
import datetime
data = pd.read_csv(path)
# sort by date (not working)
data['OrderDate'] = pd.to_datetime(data['OrderDate'])
data.sort_values(by='OrderDate')
data.reset_index(inplace=True)
# sort by month then day (not working)
data.sort_values(by='Month')
data.sort_values(by='Day')
data.reset_index(inplace=True)
# export csv
data.to_csv(fileName, index=False)
Dan O'Connell
  • 119
  • 1
  • 2
  • 8
  • 1
    you need to assign the dataframe back to a variable - and don't use [inplace](https://stackoverflow.com/a/59242208/9375102) `data = data.sort_values(by='OrderDate',ascending=False/True)` – Umar.H Feb 08 '21 at 17:52
  • 1
    Ah silly mistake, that fixed the problem thank you – Dan O'Connell Feb 08 '21 at 17:56

0 Answers0