Home > database >  Organizing data rows in order of dates Pandas Python
Organizing data rows in order of dates Pandas Python

Time:11-10

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

CodePudding user response:

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  
  • Related