0

How to sort a python data frame according to dates in the format that can be seen on the image. The output that I want to receive is the same data frame but at index 0 I would have January 2013 and the corresponding amount and at index 1 I would have February 2013 etc.

enter image description here

1
  • 1
    Please include an example of the code that you used to construct the dataframe shown in the image. Commented Nov 11, 2018 at 20:54

2 Answers 2

1
import pandas as pd
df = pd.DataFrame( {'Amount':['54241.25','54008.83','54008.82'] ,
    'Date':['05/01/2015','05/01/2017','06/01/2017']})
df['Date'] =pd.to_datetime(df.Date)
df.sort_values('Date', inplace=True)
Sign up to request clarification or add additional context in comments.

Comments

1

You just need to convert your Date column to a datetime, then you can sort the dataframe by that column

import pandas as pd

df = pd.DataFrame({'Date': ['05-2016', '05-2017', '06-2017', '01-2017', '02-2017'],
                   'Amount': [2,5,6,3,2]})

df['Date'] = pd.to_datetime(df['Date'], format='%m-%Y')
df = df.sort_values('Date').reset_index(drop=True)

Which gives:

        Date  Amount
0 2016-05-01       2
1 2017-01-01       3
2 2017-02-01       2
3 2017-05-01       5
4 2017-06-01       6

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.