- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove rows in a Pandas series with duplicate indices?
By using the duplicated() method in the pandas series constructor we can easily identify the duplicate values in the index of a series object. The method duplicated() is used to identify the duplicate values in a series object.
The duplicated() method will return a series with boolean values. Boolean value False indicates single occurrence values mean unique values. The duplicated values are indicated with boolean value True.
Here we will see how we can delete the rows of a series object with duplicate indices.
# importing pandas package import pandas as pd #create series series = pd.Series(["a", "b", "c", "d", "e"],index=[1, 2, 1, 3, 2]) print(series) # getting the index data index = series.index # removing duplicate indices separately result = series[~index.duplicated(keep="first")] print(result)
Initially, we have created a pandas series object using the pandas.Series() function with index labels [1, 2,1, 3, 2]. Then, we applied the duplicated() method on index data to identify the duplicate labels.
After that we applied the “~” to reverse the resultant boolean values and sent this data to the original series as a subset to get a new series object without any duplicate indices.
The output is mentioned below −
1 a 2 b 1 c 3 d 2 e dtype: object 1 a 2 b 3 d dtype: object
In the above output block, we can see the original series object as well as the resultant series object without duplicate labels.
Let’s take another example to remove rows of a series object with duplicate indices.
# importing package import pandas as pd import numpy as np # creating pandas series series = pd.Series(np.random.randint(1,100,10), index=["a", "b", "a", "d", "c", "e", "f", "c", "d", "e"]) print(series) # getting the index data index = series.index # removing duplicate indices separately result = series[~index.duplicated(keep="first")] print(result)
Initially, we created the series object with labeled index data and then applied the duplicated() method to identify the duplicate labels.
The output is given below −
a 66 b 73 a 83 d 63 c 23 e 56 f 55 c 22 d 26 e 20 dtype: int32 a 66 b 73 d 63 c 23 e 56 f 55 dtype: int32
The labels a, d, c, e occurred more than one time in the initial series object and those rows are removed in the resultant series object.
- How to drop duplicate rows in pandas series?
- How to remove NaN from a Pandas Series?
- Python program to remove rows with duplicate element in Matrix
- How to find and filter Duplicate rows in Pandas ?
- How to get few rows from a Series in Pandas?
- Python - Remove duplicate values from a Pandas DataFrame
- How to remove a group of elements from a pandas series object?
- Write a program in Python to remove first duplicate rows in a given dataframe
- How to sort a Pandas Series?
- How to remove a specified row From the Pandas Series Using Drop() method?
- How to remove duplicate rows in an R data frame if exists in two columns?
- How to remove duplicate rows and sort based on a numerical column an R data frame?
- Remove duplicate elements in Java with HashSet
- How to avoid inserting duplicate rows in MySQL?
- How to prevent duplicate rows in MySQL INSERT?