'Error with Pandas program to get the items of a given series not present in another given series
Basically a program to get the items of a the first series not present in second series. First Series = [1, 2, 3, 4, 5] Second Series = [2, 4, 6, 8, 10]
Expected Ans: [1,3,5]
My Code
import pandas as pd
sr1 = pd.Series([1, 2, 3, 4, 5])
sr2 = pd.Series([2, 4, 6, 8, 10])
l = []
for i in sr1:
if(i not in sr2):
l.append(i)
print(l)
My Output
[5]
Expected Output
[1,3,5]
Solution 1:[1]
Use the pandas method isin:
sr1[~sr1.isin(sr2)]
This will print the elements from sr1 that do not (~) appear in sr2.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SiP |
