Get the items which are not common of two Pandas series
Last Updated :
01 Aug, 2020
Pandas does not support specific methods to perform set operations. However, we can use the following formula to get unique items from both the sets :
Algorithm :
- Import the
Pandas
and NumPy
modules.
- Create 2
Pandas Series
.
- Find the union of the series using the
union1d()
method.
- Find the intersection of the series using the
intersect1d()
method.
- Find the difference between the union and the intersection elements. Use the
isin()
method to get the boolean list of items present in both ‘union’ and ‘intersect’.
- Print the result
import pandas as pd
import numpy as np
ser1 = pd.Series([ 1 , 2 , 3 , 4 , 5 ])
ser2 = pd.Series([ 3 , 4 , 5 , 6 , 7 ])
union = pd.Series(np.union1d(ser1, ser2))
intersect = pd.Series(np.intersect1d(ser1, ser2))
notcommonseries = union[~union.isin(intersect)]
print (notcommonseries)
|
Output :
1, 2, 6, 7
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...