6

So I have two different data-frame and I concatenated both. All columns are the same; however, the date column has all sorts of different dates in the M/D/YR format.

enter image description here dataframe dates get shuffled around later in the sequence

Is there a way to keep the whole dataframe itself and just sort the rows based on the dates in the date column. I also want to keep the format that date is in.

so basically

date        people
6/8/2015    1
7/10/2018   2
6/5/2015    0

gets converted into:

date          people
6/5/2015      0
6/8/2015      1
7/10/2018     2

Thank you!

PS: I've tried the options in the other post on this but it does not work

0

3 Answers 3

4

Trying to elaborate on what can be done: Intialize/ Merge the dataframe and convert the column into datetime type

df= pd.DataFrame({'people':[1,2,0],'date': ['6/8/2015','7/10/2018','6/5/2015',]})
df.date=pd.to_datetime(df.date,format="%m/%d/%Y")
print(df)

Output:

   date      people
0   2015-06-08  1
1   2018-07-10  2
2   2015-06-05  0

Sort on the basis of date

df=df.sort_values('date')
print(df)

Output:

    date    people
2   2015-06-05  0
0   2015-06-08  1
1   2018-07-10  2

Maintain the format again:

df['date']=df['date'].dt.strftime('%m/%d/%Y')
print(df)

Output:

    date    people
2   06/05/2015  0
0   06/08/2015  1
1   07/10/2018  2
Sign up to request clarification or add additional context in comments.

1 Comment

Since that winds up adding some leading '0's to the date, you could instead create a dummy_date column which is datetime, sort by that and then drop it to leave date truly untouched. But same basic idea :D
1

Try changing the 'date' column to pandas Datetime and then sort

import pandas as pd
df= pd.DataFrame({'people':[1,1,1,2],'date': 
['4/12/1961','5/5/1961','7/21/1961','8/6/1961']})
df['date'] =pd.to_datetime(df.date)
df.sort_values(by='date')

Output:

date       people

1961-04-12  1

1961-05-05  1

1961-07-21  1

1961-08-06  2

To get back the initial format:

df['date']=df['date'].dt.strftime('%m/%d/%y')

Output:

date    people
04/12/61    1

05/05/61    1

07/21/61    1

08/06/61    2

1 Comment

Thank you .I added one more line of code to get the initial format and also printed the output
-3

why not simply?

dataset[SortBy["date"]]

can you provide what you tried or how is your structure?

In case you need to sort in reversed order do:

dataset[SortBy["date"]][Reverse]

1 Comment

What is SortBy ? Where is it defined? and Reverse ? How come this answer has 3 upvotes?

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.