根据数据框中的列排序索引

时间:2019-11-24 00:22:26

标签: pandas dataframe indexing

我使用pd.cut创建了一个数据框,并在最后两行之后添加。我想知道如何根据“间隔”列对索引进行排序。

enter image description here

1 个答案:

答案 0 :(得分:0)

在这里使用sort_values(),然后再使用reset_index()应该足够了。假设您的数据帧称为df

df = df.sort_values(by = 'Interval', ascending = True)
df = df.reset_index(drop = True)

这将重置索引,使其随着间隔值的增加而递增。 然后,如果仍然要根据每个bin中的频率对数据框进行排序,则可以再次进行排序:

df = df.sort_values(by = 'Frequency', ascending = False)