python dataframe 按照 list 排序

小咪咪 2022-11-22 12:40 273阅读 0赞
  1. import pandas as pd
  2. s = pd.Series({'a':1,'b':2,'c':3})
  3. s
  4. s.index
  5. list_custom_new = ['d', 'c', 'b','a','e']
  6. list_custom_new
  7. d2=pd.DataFrame(s)
  8. d2=d2.reset_index()
  9. d2.columns=['t1','t2']
  10. d2
  11. d2['t1']=d2['t1'].astype('category')
  12. d2['t1']
  13. d2['t1'].cat.set_categories(list_custom_new,inplace=True)
  14. d2
  15. d2=d2.sort_values('t1',ascending=True)
  16. d2
  17. list_custom_new = ['d', 'c','a','e']
  18. d2['t1'].cat.set_categories(list_custom_new,inplace=True)
  19. d2=d2.sort_values('t1',ascending=True)
  20. d2

发表评论

表情:
评论列表 (有 0 条评论,273人围观)

还没有评论,来说两句吧...

相关阅读