【发布时间】:2019-01-12 01:27:51
【问题描述】:
我正在尝试在不更改块内顺序的情况下按块对 pandas 数据帧进行排序。
数据框包含论坛帖子、时间戳和主题名称。我已经对数据框进行了排序,以便属于同一线程的所有帖子都使用df.sort_values(['thread', 'timestamp'], inplace=True) 以正确的顺序排列。我现在想根据每个块中第一个帖子的时间戳对属于同一线程的数据块进行排序。块内的顺序应保持不变。
我目前拥有的:
post timestamp thread
0 this 2009/10/30 16:51 hello
1 be 2009/11/02 17:11 hello
2 some 2008/07/10 15:23 nice
3 text 2007/04/22 14:11 question
4 this 2007/04/24 11:03 question
5 be 2007/05/03 17:55 question
6 some 2004/09/01 09:32 game
7 text 2010/01/01 03:32 wheather
我想要什么:
post timestamp thread
6 some 2004/09/01 09:32 game
3 text 2007/04/22 14:11 question
4 this 2007/04/24 11:03 question
5 be 2007/05/03 17:55 question
2 some 2008/07/10 15:23 nice
0 this 2009/10/30 16:51 hello
1 be 2009/11/02 17:11 hello
7 text 2010/01/01 03:32 wheather
有没有办法做到这一点?
【问题讨论】:
标签: python pandas dataframe group-by pandas-groupby