我有以下数据框:
latitude longitude timestamp
name
testschip-1 51.822872 4.905615 2019-08-01 00:00:00
testschip-1 51.822876 4.905613 2019-08-01 00:00:57
testschip-1 51.822872 4.905613 2019-08-01 00:00:20
testschip-1 51.822880 4.905627 2019-08-01 00:00:27
testschip-1 51.822872 4.905613 2019-08-01 00:00:09
testschip-1 51.822872 4.905613 2019-08-01 00:00:39
testschip-1 51.822876 4.905613 2019-08-01 00:00:38
testschip-10 51.824749 4.965562 2019-08-01 00:00:56
testschip-10 51.827072 4.952107 2019-08-01 00:00:47
testschip-10 51.826794 4.954065 2019-08-01 00:00:19
testschip-10 51.826702 4.954648 2019-08-01 00:00:08
testschip-11 51.819588 4.962135 2019-08-01 00:00:57
testschip-11 51.819584 4.962165 2019-08-01 00:00:38
testschip-11 51.819588 4.962172 2019-08-01 00:00:08
testschip-11 51.819588 4.962122 2019-08-01 00:00:17
每个名称(在索引上)我想按层次结构对时间戳进行排序。 如果我这样做:
df = df.sort_values(by='timestamp',ascending=True)
我按时间排序,但是我在索引上丢失了排序的名称。如何保留排序的索引并按时间对这些索引进行排序?
答案 0 :(得分:1)
df = df.sort_values(by=['name', 'timestamp'], ascending=True)
by
参数可以是字符串或字符串列表,请参见https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.sort_values.html