Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-对两列上的数据帧排序不起作用_Python_Sorting_Pandas - Fatal编程技术网

Python-对两列上的数据帧排序不起作用

Python-对两列上的数据帧排序不起作用,python,sorting,pandas,Python,Sorting,Pandas,我在基于两列对数据进行排序时遇到问题 一般来说,我希望在数据帧上设置一个时区,并进行时间更改。不幸的是 tz.localize("Europe/Berlin", infer_dst=True) 由于数据未正确排序而引发错误(至少我认为这是原因…) 因此,我尝试对以下数据帧进行排序: index value test1 test2 2015-10-25 02:00:00 10 2015-10-25 02:00:00

我在基于两列对数据进行排序时遇到问题

一般来说,我希望在数据帧上设置一个时区,并进行时间更改。不幸的是

tz.localize("Europe/Berlin", infer_dst=True) 
由于数据未正确排序而引发错误(至少我认为这是原因…)

因此,我尝试对以下数据帧进行排序:

index                value   test1                 test2
2015-10-25 02:00:00   10     2015-10-25 02:00:00   0
2015-10-25 02:00:00   20     2015-10-25 02:00:00   1
2015-10-25 02:15:00   30     2015-10-25 02:15:00   0
2015-10-25 02:15:00   40     2015-10-25 02:15:00   1
2015-10-25 02:30:00   60     2015-10-25 02:30:00   0
2015-10-25 02:30:00   70     2015-10-25 02:30:00   1
2015-10-25 02:45:00   80     2015-10-25 02:45:00   0
2015-10-25 02:45:00   90     2015-10-25 02:45:00   1
要获得以下输出

index                value   test1                 test2
2015-10-25 01:45:00   5     2015-10-25 01:45:00   0
2015-10-25 02:00:00   10     2015-10-25 02:00:00   0
2015-10-25 02:15:00   30     2015-10-25 02:15:00   0    
2015-10-25 02:30:00   60     2015-10-25 02:30:00   0
2015-10-25 02:45:00   80     2015-10-25 02:45:00   0
2015-10-25 02:00:00   20     2015-10-25 02:00:00   1
2015-10-25 02:15:00   40     2015-10-25 02:15:00   1
2015-10-25 02:30:00   70     2015-10-25 02:30:00   1
2015-10-25 02:45:00   90     2015-10-25 02:45:00   1
2015-10-25 03:00:00   90     2015-10-25 03:00:00   1
我试过:

df.sort(["test1","test2"],ascending=[1, 1])
然而,由于某些原因,订单没有改变。有什么想法吗


您好,我扩展了所需的输出

您只需更改列的顺序并按
[“test2”、“test1”]
排序即可:


您好,很遗憾,这将无法提供所需的输出!只有当数据帧仅由这8行组成,但有40000行时,这才有效。你的建议意味着标有“1”的季度将被排到最后,而不是正确的位置好的,那么你能为产出创造一个好的例子吗?我在你的问题中看不到“正确的地方”的定义,你确定吗?您想要分开的排序列
test1
test2
,然后用这些排序列覆盖数据帧吗?然后,您可以使用
df['test1']=df['test1'].sort_values().values
df['test2']=df['test2'].sort_values().values
。请注意完全覆盖。我想根据索引(a复制到测试列)和列“test2”对整个数据帧进行排序。基本上,我需要两个季度:02:00-02:45。好吧,您的输入数据集已经按test1、test2排序。对不起,可能我遗漏了什么。因为如果我想按
test1
-
打印df.sort\u值([“test1”],升序=True)
-这是未排序的
test2
。如果我按
test2
打印df值([“test2”],升序=True)
进行排序,这与
打印df.sort值([“test2”,“test1”],升序=[1,1])相同。
-
test1
未排序。我认为这一切都是正确的。。。不可能在不覆盖的情况下对两列进行排序。
>>> df.sort(["test2","test1"],ascending=[1, 1])
                 index  value                test1  test2
0  2015-10-25 02:00:00     10  2015-10-25 02:00:00      0
2  2015-10-25 02:15:00     30  2015-10-25 02:15:00      0
4  2015-10-25 02:30:00     60  2015-10-25 02:30:00      0
6  2015-10-25 02:45:00     80  2015-10-25 02:45:00      0
1  2015-10-25 02:00:00     20  2015-10-25 02:00:00      1
3  2015-10-25 02:15:00     40  2015-10-25 02:15:00      1
5  2015-10-25 02:30:00     70  2015-10-25 02:30:00      1
7  2015-10-25 02:45:00     90  2015-10-25 02:45:00      1