close

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

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to reorder indexed rows based on a list in Pandas data frame in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

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

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

    You could set index on predefined order using reindex like
    In [14]: df.reindex(["Z", "C", "A"]) Out[14]: company Amazon Apple Yahoo

  2. reorder indexed rows based on a list in Pandas data frame

    You could set index on predefined order using reindex like
    In [14]: df.reindex(["Z", "C", "A"]) Out[14]: company Amazon Apple Yahoo

Method 1

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)

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read