w3resource

Pandas Data Series: Get the items which are not common of two given series

Pandas: Data Series Exercise-17 with Solution

Write a Pandas program to get the items which are not common of two given series.

Sample Solution :

Python Code :

import pandas as pd
import numpy as np
sr1 = pd.Series([1, 2, 3, 4, 5])
sr2 = pd.Series([2, 4, 6, 8, 10])
print("Original Series:")
print("sr1:")
print(sr1)
print("sr2:")
print(sr2)
print("\nItems of a given series not present in another given series:")
sr11 = pd.Series(np.union1d(sr1, sr2))
sr22 = pd.Series(np.intersect1d(sr1, sr2))
result = sr11[~sr11.isin(sr22)]
print(result)

Sample Output:

Original Series:
sr1:
0    1
1    2
2    3
3    4
4    5
dtype: int64
sr2:
0     2
1     4
2     6
3     8
4    10
dtype: int64

Items of a given series not present in another given series:
0     1
2     3
4     5
5     6
6     8
7    10
dtype: int64          

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to get the items of a given series not present in another given series.
Next: Write a Pandas program to compute the minimum, 25th percentile, median, 75th, and maximum of a given series.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Function argument unpacking in Python.

Example:

def tips_func(x, y, z):
  print(x, y, z)
  
tuple_val = (2, 0, 2)
dict_val = {'x': 3, 'y': 2, 'z': 1}
tips_func(*tuple_val)
tips_func(**dict_val)

Output:

2 0 2
3 2 1