2

How would I be organize the csv file with pandas so that it organizes the rows from oldestto to newest date. The date values in data.csv are unorganized what code would I be able to implement so I could get the Expected Output below?

Code:

data = pd.read_csv('data.csv')
dates= pd.to_datetime(data['Date'].to_list())

data.csv file contents:

Unix Timestamp,Date,Symbol,Open,High,Low,Close,Volume
1635686100,2021-10-31 13:15:00,BTCUSD,60620.0,60633.0,60565.0,60568.0,1.3977284440628714
1635686220,2021-10-31 13:17:00,BTCUSD,60638.0,60640.0,60636.0,60638.0,0.4357009185659157
1635686160,2021-10-31 13:16:00,BTCUSD,60568.0,60640.0,60568.0,60638.0,3.9771881707839967

Expected Output:

Unix Timestamp,Date,Symbol,Open,High,Low,Close,Volume
1635686220,2021-10-31 13:17:00,BTCUSD,60638.0,60640.0,60636.0,60638.0,0.4357009185659157
1635686160,2021-10-31 13:16:00,BTCUSD,60568.0,60640.0,60568.0,60638.0,3.9771881707839967
1635686100,2021-10-31 13:15:00,BTCUSD,60620.0,60633.0,60565.0,60568.0,1.3977284440628714

1 Answer 1

3

Convert column Date to datetimes by parameter parse_dates first and then use DataFrame.sort_values:

data = pd.read_csv('data.csv', parse_dates=['Date'])

data = data.sort_values('Date', ascending=False, ignore_index=True)
print (data)
   Unix Timestamp                Date  Symbol     Open     High      Low  \
0      1635686220 2021-10-31 13:17:00  BTCUSD  60638.0  60640.0  60636.0   
1      1635686160 2021-10-31 13:16:00  BTCUSD  60568.0  60640.0  60568.0   
2      1635686100 2021-10-31 13:15:00  BTCUSD  60620.0  60633.0  60565.0   

     Close    Volume  
0  60638.0  0.435701  
1  60638.0  3.977188  
2  60568.0  1.397728  
Sign up to request clarification or add additional context in comments.

4 Comments

Downvoter, if there's something wrong with my answer, please let me know, so I can correct it. Thanks.
I think just the usual random DV, might be reverted later if this is a bot, in the meantime get +1 ;)
Imma upvote it because this is legit solution :)
upvoted as well

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.