Python Pandas: Get the items of a given series not present in another given series

Python Pandas: Data Series Exercise-16 with Solution

Write a Pandas program to get the items of a given series not present in another given series.

Sample Solution :

Python Code :

import pandas as pd
sr1 = pd.Series([1, 2, 3, 4, 5])
sr2 = pd.Series([2, 4, 6, 8, 10])
print("Original Series:")
print("\nItems of sr1 not present in sr2:")
result = sr1[~sr1.isin(sr2)]

Sample Output:

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

Items of sr1 not present in sr2:
0    1
2    3
4    5
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 create the mean and standard deviation of the data of a given Series.
Next: Write a Pandas program to get the items which are not common of two given series.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

Python: Tips of the Day

Python: Cache results with decorators

There is a great way to cache functions with decorators in Python. Caching will help save time and precious resources when there is an expensive function at hand.

Implementation is easy, just import lru_cache from functools library and decorate your function using @lru_cache.

from functools import lru_cache

def fibo(a):
    if a <= 1:
        return a
        return fibo(a-1) + fibo(a-2)

for i in range(20):
    print(fibo(i), end="|")

print("\n\n", fibo.cache_info())



 CacheInfo(hits=36, misses=20, maxsize=None, currsize=20)