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()看看