How to reorder indexed rows based on a list in Pandas data frame

后端 未结 1 793
你的背包
你的背包 2020-12-04 11:16

I have a data frame that looks like this:

company  Amazon  Apple  Yahoo
name
A             0    130      0
C           173      0      0
Z             0              


        
相关标签:
1条回答
  • 2020-12-04 11:32

    You could set index on predefined order using reindex like

    In [14]: df.reindex(["Z", "C", "A"])
    Out[14]:
    company  Amazon  Apple  Yahoo
    Z             0      0    150
    C           173      0      0
    A             0    130      0
    

    However, if it's alphabetical order, you could use sort_index(ascending=False)

    In [12]: df.sort_index(ascending=False)
    Out[12]:
    company  Amazon  Apple  Yahoo
    name
    Z             0      0    150
    C           173      0      0
    A             0    130      0
    

    Like pointed below, you need to assign it to some variable

    In [13]: df = df.sort_index(ascending=False)
    
    0 讨论(0)
提交回复
热议问题