pandas 按照指定顺序进行列排序
将排序列设置成“category”数据类型
list_custom = ['b', 'a', 'c']
df['words'] = df['words'].astype('category')
df['words'].cat.reorder_categories(list_custom, inplace=True)
df.sort_values('words', inplace=True)
出处:博客园:http://www.cnblogs.com/lemonbit/
这篇博客介绍了如何使用pandas库按照指定顺序对数据框的列进行排序。首先,将需要排序的列转换为'category'类型,然后使用`cat.reorder_categories()`方法设置自定义排序顺序,最后调用`sort_values()`函数实现列排序。这种方法适用于需要特定列顺序的情况。
5万+

被折叠的 条评论
为什么被折叠?



