Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.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_Pandas_Sorting_Concat - Fatal编程技术网

Python 对连接的数据帧进行排序

Python 对连接的数据帧进行排序,python,pandas,sorting,concat,Python,Pandas,Sorting,Concat,我想连接两个熊猫数据帧A和B,然后按两列对它们进行排序'geohash'和'timestamp' A geohash timestamp 0 a2a 15 1 b3a 14 B geohash timestamp 0 a2b 15 1 b3b 14 之后 AB = pd.concat([A,B],ignore_index=True) AB.sort_values(['geohash','timestamp']) 我

我想连接两个熊猫数据帧
A
B
,然后按两列对它们进行排序
'geohash'
'timestamp'

A
    geohash  timestamp
0   a2a      15
1   b3a      14

B
    geohash  timestamp
0   a2b      15
1   b3b      14
之后

AB = pd.concat([A,B],ignore_index=True)
AB.sort_values(['geohash','timestamp'])
我想

AB
    geohash  timestamp
0   a2a      15
1   a2b      15
2   b3a      14
3   b3b      14
但我明白了

AB
    geohash  timestamp
0   a2a      15
1   b3a      14
2   a2b      14
3   b3b      15

熊猫为什么不对整个数据帧进行排序
AB

排序\u值
。所以当你跑步时:

AB.sort_values(['geohash','timestamp'])
它不是更新
AB
,而是返回一份副本

AB.sort_values(['geohash','timestamp'], inplace=True)
将更新
AB

或者,您可以将排序后的数据帧分配给一个新变量

AB_sorted = AB.sort_values(['geohash','timestamp'])
AB_sorted 

geohash timestamp
0   a2a 15
2   a2b 15
1   b3a 14
3   b3b 15

这是我的第一个问题,我想知道如何改进:)您的输出似乎有点不正确。排序后,我想按顺序创建索引。我该怎么做?@AkhilMittal你可以试试df.reset_index()看看