Search code examples
pythonpandaslistdataframeisin

How to get words matches with it's count using pandas


i have 2 Dataframes like

set1 = ['a','b','c','d','e','f','g','h','i','j']
set2 = ['a','b','b','c','c','f','h','j','k']

df1 = pd.DataFrame(set1, columns=['name'])
df2 = pd.DataFrame(set2, columns=['name'])

i want to compare these 2 Dataframes without forloop and get a output like

df3 = ['a=1','b=2','c=2','f=1','h=1','j=1']

which way i can use for getting this output any examples .?? here i want only the items from df2 that is presented on df1

only print that items only like df3 and i need to get the count also ( is that not able on pandas no problem i just need a list like df3 that is also fine i used merge func but it's showing inner , outer, left join, right join, these methods so not getting any idea which way is better to do this


Solution

  • Hope this helps!

    import pandas as pd
    df1 = pd.DataFrame({'df1_col':['a','b','c','d','e','f','g','h','i','j']})
    df2 = pd.DataFrame({'df2_col':['a','b','b','c','c','f','h','j','k']})
    
    df2[df2['df2_col'].isin(df1['df1_col'])]['df2_col'].value_counts()