- 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
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
Advertisements