Categories: NumpyPandasPython

Working with duplicate values in Pandas Series with Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental level, click here for more details.

When a Pandas Series data object is created in Python, is values can be evaluated with respect to duplicate values. Pandas provides several handy functions dealing with duplicate values in Series. unique() returns unique values of the object, value_counts() will list frequency of each unique value, and isin() will return a boolean Series in terms of elements of the Sereis can be found in the specified list. Next we will show you how to implement these functions in Python IDE.

#Import Pandas and Numpy module
import pandas as pd
import numpy as np
#create a Series with duplicate values
S1 = pd.Series([32,19,201,7,32,19])
S1
#output
0     32
1     19
2    201
3      7
4     32
5     19
dtype: int64
#return unique values of the Series
S1.unique()
#result is a Numpy array
array([ 32,  19, 201,   7], dtype=int64)
#count frequency of unique values in the Series
S1.value_counts()
#output, result is a new Series
32     2
19     2
201    1
7      1
Name: count, dtype: int64
#check values of Series are in the specified list
S1.isin([32,19])
#result is a Series with boolean values
0     True
1     True
2    False
3    False
4     True
5     True
dtype: bool
#isin() can be used to filter values, and store to a new Series
S1[S1.isin([32,19])]
#result is a new Series, with fewer elements than original one
0    32
1    19
4    32
5    19
dtype: int64

You can also watch videos on our YouTube channel for more understanding of Python programming skills.

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago