
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Write a Python code to swap last two rows in a given dataframe
Assume you have dataframe and the result for swapping last two rows,
Before swapping Name Age Maths Science English 0 David 13 98 75 79 1 Adam 12 59 96 45 2 Bob 12 66 55 70 3 Alex 13 95 49 60 4 Serina 12 70 78 80 After swapping Name Age Maths Science English 0 David 13 98 75 79 1 Adam 12 59 96 45 2 Bob 12 66 55 70 3 Serina 12 70 78 80 4 Alex 13 95 49 60
Solution
To solve this, we will follow the approach given below −
Define a dataframe
Create temp data to store last row. It is defined below,
temp = df.iloc[-1]
Swap second row values to first row and assign temp data to second row. It is defined below,
df.iloc[-1] = df.iloc[-2] df.iloc[-2] = temp
Example
Let’s see the below implementation to get better understanding −
import pandas as pd data = {'Name': ['David', 'Adam', 'Bob', 'Alex', 'Serina'], 'Age' : [13,12,12,13,12], 'Maths': [98, 59, 66, 95, 70], 'Science': [75, 96, 55, 49, 78], 'English': [79, 45, 70, 60, 80]} df = pd.DataFrame(data) print("Before swapping\n",df) temp = df.iloc[-1] df.iloc[-1] = df.iloc[-2] df.iloc[-2] = temp print("After swapping\n",df)
Output
Before swapping Name Age Maths Science English 0 David 13 98 75 79 1 Adam 12 59 96 45 2 Bob 12 66 55 70 3 Alex 13 95 49 60 4 Serina 12 70 78 80 After swapping Name Age Maths Science English 0 David 13 98 75 79 1 Adam 12 59 96 45 2 Bob 12 66 55 70 3 Serina 12 70 78 80 4 Alex 13 95 49 60
- Related Articles
- Write a Python code to combine two given series and convert it to a dataframe
- Write a Python code to rename the given axis in a dataframe
- Write a Python code to filter palindrome names in a given dataframe
- Write a program in Python to remove first duplicate rows in a given dataframe
- Write a Python code to fill all the missing values in a given dataframe
- Write a Python code to select any one random row from a given DataFrame
- Write a program in Python to select any random odd index rows in a given DataFrame
- Write a Python code to find the second lowest value in each column in a given dataframe
- Write a Python program to reshape a given dataframe in different ways
- Write a program in Python to convert a given dataframe to a LaTex document
- Write a program in Python to localize Asian timezone for a given dataframe
- Write a program in Python to read CSV data from a file and print the total sum of last two rows
- Write a Python program to find the mean absolute deviation of rows and columns in a dataframe
- Write a program in Python to print dataframe rows as orderDict with a list of tuple values
- Write a program in Python to transpose the index and columns in a given DataFrame

Advertisements