3

I'm having trouble sorting a csv file which has in its second column the UTC time as: 2010-01-01 00:00:00

I have a file that is like this:

name        utc_time             longitude    latitude
A           2010-01-01 00:00:34  23           41
B           2011-01-01 10:00:00  26           44
C           2009-01-01 03:00:00  34           46
D           2012-01-01 00:00:00  31           47
E           2010-01-01 04:00:00  44           48
F           2013-01-01 14:00:00  24           41

Which I want it to be outputted in a csv file keeping the same structure but sorted by date:

Output:

name        utc_time             longitude    latitude
C           2009-01-01 03:00:00  34           46
A           2010-01-01 00:00:34  23           41
E           2010-01-01 04:00:00  44           48
B           2011-01-01 10:00:00  26           44
D           2012-01-01 00:00:00  31           47
F           2013-01-01 14:00:00  24           41

I'm actually trying this:

fileEru = pd.read_csv("input.csv")
fileEru = sorted(fileEru, key = lambda row: datetime.strptime(row[1],'%Y-%m-%d %H:%M:%S'), reverse=True)
fileEru.to_csv("output.csv")

But it doesn't work.

1
  • Why it doesn't work? Please add an explanation to your question. Commented May 20, 2018 at 17:02

1 Answer 1

2

try this:

(pd.read_csv("input.csv", parse_dates=['utc_time'])
   .sort_values('utc_time')
   .to_csv("output.csv", index=False))
Sign up to request clarification or add additional context in comments.

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.