- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reverse the Rows of a Pandas Data Frame?
We will see here how to reverse the rows of a Pandas Dataframe. Pandas is an open-source Python Library providing high-performance data manipulation and analysis tool using its powerful data structures. A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns.
Reverse the rows of a Pandas Data Frame using indexing
Example
In this example, we will reverse the rows of a dataframe using [::-1] −
import pandas as pd # Create a Dictionary dct = {'Rank':[1,2,3,4,5], 'Points':[100,87, 80,70, 50]} # Create a DataFrame from Dictionary elements using pandas.dataframe() df = pd.DataFrame(dct) print("DataFrame = \n",df) # Reverse the DataFrame using indexing print("\nReverse the DataFrame = \n",df[::-1])
Output
DataFrame = Rank Points 0 1 100 1 2 87 2 3 80 3 4 70 4 5 50 Reverse the DataFrame = Rank Points 4 5 50 3 4 70 2 3 80 1 2 87 0 1 100
Reverse the rows of a Pandas Data Frame using reindex()
Example
In this example, we will reverse the rows of a dataframe using the reindex() −
import pandas as pd # Create a Dictionary dct = {'Rank':[1,2,3,4,5], 'Points':[100,87, 80,70, 50]} # Create a DataFrame from Dictionary elements using pandas.dataframe() df = pd.DataFrame(dct) print("DataFrame = \n",df) # Reverse the DataFrame using reindex() print("\nReverse the DataFrame = \n",df.reindex(index=df.index[::-1]))
Output
DataFrame = Rank Points 0 1 100 1 2 87 2 3 80 3 4 70 4 5 50 Reverse the DataFrame = Rank Points 4 5 50 3 4 70 2 3 80 1 2 87 0 1 100
Reverse the rows of a Pandas Data Frame using iloc
Example
In this example, we will reverse the rows of a dataframe using the iloc −
import pandas as pd # Create a Dictionary dct = {'Rank':[1,2,3,4,5], 'Points':[100,87, 80,70, 50]} # Create a DataFrame from Dictionary elements using pandas.dataframe() df = pd.DataFrame(dct) print("DataFrame = \n",df) # Reverse the DataFrame using iloc print("\nReverse the DataFrame = \n",df.iloc[::-1])
Output
DataFrame = Rank Points 0 1 100 1 2 87 2 3 80 3 4 70 4 5 50 Reverse the DataFrame = Rank Points 4 5 50 3 4 70 2 3 80 1 2 87 0 1 100
Reverse the rows of a Pandas Data Frame using sort_index()
Example
In this example, we will reverse the rows of a dataframe using the sort_index() method. In the parameter, we can set the order i.e. ascending False or True −
import pandas as pd # Create a Dictionary dct = {'Rank':[1,2,3,4,5], 'Points':[100,87, 80,70, 50]} # Create a DataFrame from Dictionary elements using pandas.dataframe() df = pd.DataFrame(dct) print("DataFrame = \n",df) # Reverse the DataFrame using sort_index() print("\nReverse the DataFrame = \n",df.sort_index(ascending=False))
Output
DataFrame = Rank Points 0 1 100 1 2 87 2 3 80 3 4 70 4 5 50 Reverse the DataFrame = Rank Points 4 5 50 3 4 70 2 3 80 1 2 87 0 1 100
Reverse the rows of a Pandas Data Frame using reset_index()
Example
Here, we will see another way to reverse the rows of a DataFrame. This also resets the index after reversing the DataFrame. Let us see the example −
import pandas as pd # Create a Dictionary dct = {'Rank':[1,2,3,4,5], 'Points':[100,87, 80,70, 50]} # Create a DataFrame from Dictionary elements using pandas.dataframe() df = pd.DataFrame(dct) print("DataFrame = \n",df) # Reverse the DataFrame using reset_index() print("\nReverse the DataFrame = \n",df[::-1].reset_index())
Output
DataFrame = Rank Points 0 1 100 1 2 87 2 3 80 3 4 70 4 5 50 Reverse the DataFrame = index Rank Points 0 4 5 50 1 3 4 70 2 2 3 80 3 1 2 87 4 0 1 100