简体   繁体   中英

How does pandas sort_values function work when ascending?

Does the argument "ascending" for pandas sort_values function return values from least -> greatest?

I thought that sorting ascending=True would give back a list like 10, 10, 10, 4, 1.

However, it's the reverse when I am sorting. I don't know if I'm misunderstanding or something is wrong with my code.

#create new column that concatenates date and time for sorting
SQLdf['P2_TIMESTAMP'] = [int(str(date) + str(time)) for date, time in zip(SQLdf['SCAN_P2_DTE'], SQLdf['SCAN_P2_TIME'])]

...


SQLdf.sort_values(['P2_TIMESTAMP'], inplace=True, ascending=True)

The output sorts my data from least -> greatest (eg. 201906201524, then 201906201920). I can make ascending=False and get the order I want, but this seems odd.

Yes by default pandas sort_values sorts the values in ascending/increasing order . So, if you have a list like 10, 10, 10, 4, 1 it would give 1, 4, 10, 10, 10 as the result. If you want to get the result as you want you need to set ascending = False which I see you have tried already. This means that you are sorting the values in descending order which means decreasing(highest > smallest). If you are getting confused, just think of ascending as climbing a hill until you reach the peak which means a higher point than the starting point and descending as coming down from the highest point of that hill. Check out the official documentation for more technical info: Link Hope this helps!

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