简体   繁体   English

升序时,pandas sort_values 函数如何工作?

[英]How does pandas sort_values function work when ascending?

Does the argument "ascending" for pandas sort_values function return values from least -> greatest? pandas sort_values 函数的参数“升序”是否从最小 -> 最大返回值?

I thought that sorting ascending=True would give back a list like 10, 10, 10, 4, 1.我认为排序升序 = True 会返回一个像 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).输出将我的数据从最小 -> 最大(例如 201906201524,然后是 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 .,默认情况下, pandas sort_values 按升序/递增顺序对值进行排序 So, if you have a list like 10, 10, 10, 4, 1 it would give 1, 4, 10, 10, 10 as the result.所以,如果你有一个像 10, 10, 10, 4, 1 这样的列表,它会给出 1, 4, 10, 10, 10 作为结果。 If you want to get the result as you want you need to set ascending = False which I see you have tried already.如果你想得到你想要的结果,你需要设置上升 = False,我看到你已经尝试过了。 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!查看官方文档以获取更多技术信息: 链接希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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