I have two data frames like below:-
import pandas
import numpy as np
data = data = {'Name': ['Tom', 'Joseph', 'Krish', 'John','rack','rox','selena','jha'], 'Age': [20, 21,18,20,30,20,18,20]}
df = pd.DataFrame(data)
print(df)
# Output :
# Name Age
# 0 Tom 20
1 Joseph 21
2 Krish 18
3 John 20
4 rack 30
5 rox 20
6 selena 18
7 jha 20
data = {'Named': ['Raj', 'kir', 'cena','ang'], 'Age': [20, 21,18,30]}
df1 = pd.DataFrame(data)
print(df1)
# Output :
# Named Age
# 0 Raj 20
# 1 kir 21
# 2 cena 18
# 3 ang 30
Now I want to filter the age column of df with age column of df1. The output should also include the duplicate values. I tried to use the simple filter which is excluding the duplicates, its only giving the unique values. How do I filter which includes the duplicate values as well?
My code and output:
res = df1[df1['Age'].isin(df['Age'])]
Named Age
0 Raj 20
1 kir 21
2 cena 18
3 ang 30
Execpted output:-
Named Age
0 Raj 20
1 kir 21
2 cena 18
3 Raj 20
4 ang 30
5 Raj 20
6 cena 18
7 Raj 20
CodePudding user response:
It looks like you want a right merge:
df1.merge(df[['Age']].dropna(), on='Age', how='right')
output:
Named Age
0 Raj 20
1 kir 21
2 cena 18
3 Raj 20
4 ang 30
5 Raj 20
6 cena 18
7 Raj 20