
- 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 program in Python to remove first duplicate rows in a given dataframe
Assume, you have a dataframe and the result for removing first duplicate rows are,
Id Age 0 1 12 3 4 13 4 5 14 5 6 12 6 2 13 7 7 16 8 3 14 9 9 15 10 10 14
Solution
To solve this, we will follow the steps given below −
Define a dataframe
Apply drop_duplicates function inside Id and Age column then assign keep initial value as ‘last’.
df.drop_duplicates(subset=['Id','Age'],keep='last')
Store the result inside same dataframe and print it
Example
Let’s see the below implementation to get a better understanding −
import pandas as pd df = pd.DataFrame({'Id':[1,2,3,4,5,6,2,7,3,9,10], 'Age':[12,13,14,13,14,12,13,16,14,15,14] }) print("DataFrame is:\n",df) df = df.drop_duplicates(subset=['Id','Age'],keep='last') print("Dataframe after removing first duplicate rows:\n", df)
Output
DataFrame is: Id Age 0 1 12 1 2 13 2 3 14 3 4 13 4 5 14 5 6 12 6 2 13 7 7 16 8 3 14 9 9 15 10 10 14 Dataframe after removing first duplicate rows: Id Age 0 1 12 3 4 13 4 5 14 5 6 12 6 2 13 7 7 16 8 3 14 9 9 15 10 10 14
- Related Articles
- Write a program in Python to select any random odd index rows in a given DataFrame
- Write Python program to find duplicate rows in a binary matrix
- Write a Python code to swap last two rows in a given dataframe
- Write a program in Python to remove one or more than one columns in a given DataFrame
- Program to remove duplicate characters from a given string in Python
- Python program to remove rows with duplicate element in Matrix
- Write a Python program to reshape a given dataframe in different ways
- Python - Remove duplicate values from a Pandas DataFrame
- Write a Python program to find the maximum value from first four rows in a given series
- 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 transpose the index and columns in a given DataFrame
- Program to remove duplicate entries in a list in Python
- 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

Advertisements