Intersection between pandas columns

Asked

Viewed 398 times

3

Considering two dataframes like the ones below:

import pandas as pd
df = pd.DataFrame({'id':[3,6,9,12,15]})
df1 = pd.DataFrame({'id': [2,4,6,8,10,12,14]})

How to account for the intersection between df and df1? That is, how many elements of df are also in df1

3 answers

4


df.loc[df['id'].isin(df1['id'])]

2

You can use the function merge to pick up the intersection and then count the elements:

pd.merge(df, df1, on = "id", how="inner").count()

0

Taking advantage of the question, would these codes serve for totally different dataframes in both the number of rows and the number of columns? And if we were to compare the lines with texts between the two dataframes it would also serve?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.