4

I try to sort dataframe shown below by date using df.sort_values(by='date') however it doesn't work. Any ideas how can I do this to be sure that it is sorted properly?

     symbol        date     open    close     high      low
0      GOOG  2007-01-03   232.77   233.56   238.09   230.32
1      GOOG  2007-01-05   241.01   243.35   243.51   238.82
2      GOOG  2007-01-04   234.27   241.39   241.73   233.94

...
2692   GOOG  2017-11-30  1022.37  1021.41  1028.49  1015.00
2693   GOOG  2017-11-29  1042.68  1021.66  1044.08  1015.65
2694   GOOG  2017-12-01  1015.80  1010.17  1022.49  1002.02
5
  • 2
    I haven't down-voted, but please note, "It doesn't work" is not an adequate problem specification. Please read “It’s not working” is not helpful Commented Dec 4, 2017 at 21:25
  • @juanpa.arrivillaga I just want to know how can I sort it, I only mentioned that df.sort_values(by='date') doesn't work. I don't have an idea why. Commented Dec 4, 2017 at 21:27
  • OK. See, if I were going to answer this, I would say "just use df.sort_values", but apparently, that "doesn't work", well, how does it not work? Provide a minimal reproducible example Commented Dec 4, 2017 at 21:28
  • 1
    df.sort_values(by='date') returns sorted DF, but it doesn't sort in place. So either use: df = df.sort_values(by='date') or df.sort_values(by='date', inplace=True) Commented Dec 4, 2017 at 21:32
  • @MaxU I think that it was a problem. It seems to me that df = df.sort_values(by='date') works properly. If you add an answer I will accept it. Commented Dec 4, 2017 at 21:35

3 Answers 3

7

df.sort_values() returns sorted DF, but it doesn't sort in place.

So either use:

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

or

df.sort_values(by='date', inplace=True)
Sign up to request clarification or add additional context in comments.

Comments

2

Try

df['Date']=pd.to_datetime(df.Date)
df.sort_values(['Date'])

1 Comment

AttributeError: 'DataFrame' object has no attribute 'sort'
1

If you have a dataframe like this below:

df = pd.DataFrame({'col1' : ['A', 'A', 'B', np.nan, 'D', 'C'],
'col2' : [2, 1, 9, 8, 7, 4],
'col3': [0, 1, 9, 4, 2, 3], })

This is how you sort it:

df = df.sort_values(by=['col1'])

Similar problem with solution on stackoverflow: how to sort pandas dataframe from one column Panda reference : http://pandas.pydata.org/pandas-docs/version/0.19.2/generated/pandas.DataFrame.sort.html

Comments

Your Answer

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