Saturday, September 27, 2025
HomeLanguagesGet the items which are not common of two Pandas series

Get the items which are not common of two Pandas series

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 :

  A \cup  B - (A \cap B)

Algorithm :

  1. Import the Pandas and NumPy modules.
  2. Create 2 Pandas Series.
  3. Find the union of the series using the union1d() method.
  4. Find the intersection of the series using the intersect1d() method.
  5. 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’.
  6. Print the result




# import the modules
import pandas as pd 
import numpy as np
  
# create the series 
ser1 = pd.Series([1, 2, 3, 4, 5])
ser2 = pd.Series([3, 4, 5, 6, 7])
  
# union of the series
union = pd.Series(np.union1d(ser1, ser2))
  
# intersection of the series
intersect = pd.Series(np.intersect1d(ser1, ser2))
  
# uncommon elements in both the series 
notcommonseries = union[~union.isin(intersect)]
  
# displaying the result
print(notcommonseries)


Output :

1, 2, 6, 7
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32322 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6690 POSTS0 COMMENTS
Nicole Veronica
11857 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11913 POSTS0 COMMENTS
Shaida Kate Naidoo
6805 POSTS0 COMMENTS
Ted Musemwa
7073 POSTS0 COMMENTS
Thapelo Manthata
6763 POSTS0 COMMENTS
Umr Jansen
6768 POSTS0 COMMENTS