简体   繁体   中英

Pandas: Sort dataframe by date string without converting

Take this simple dataframe:

df = pd.DataFrame({
    'date':['1/15/2017', '2/15/2017','10/15/2016', '3/15/2017'], 
    'int':[2,3,1,4]
})

I'd like to sort it by the date, and then save it to a CSV without having to:

  1. Convert dates using pd.to_datetime(df['date'])
  2. Sort the dataframe using .sort_values('date')
  3. Convert dates back to .strftime('%-m/%-d/%Y')

And instead do something like this (which of course, doesn't work):

df.apply(pd.to_dataframe(df['date']).sort_values(by = 'date', inplace = True)

Output:

         date  kw
2  10/15/2016   1
0   1/15/2017   2
1   2/15/2017   3
3   3/15/2017   4

Is this possible, or should I just stick with the 3-step process?

numpy 's argsort returns the permutation necessary for sorting an array. We can take advantage of that using iloc . So by converting the dates using pd.to_datetime then subsequently grabbing the values and calling argsort we've done all that we need to sort the original dataframe without changing any of it's columns.

df.iloc[pd.to_datetime(df.date).values.argsort()]

         date  int
2  10/15/2016    1
0   1/15/2017    2
1   2/15/2017    3
3   3/15/2017    4

you can use .assign() method:

In [22]: df.assign(x=pd.to_datetime(df['date'])).sort_values('x').drop('x', 1)
Out[22]:
         date  int
2  10/15/2016    1
0   1/15/2017    2
1   2/15/2017    3
3   3/15/2017    4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM