-3

I have two dataFrame, both of them have name column, I want to make new dataframe of dataframeA have and dataframeB don't have

dataframeA
id     name
 1      aaa
 2      bbbb
 3      cccc
 4      gggg

dataframeB
id     name
 1      ddd
 2      aaa
 3      gggg

new dataframe

id     name
 1      bbbb
 2      cccc

3 Answers3

0

If I understand correctly, ou can merge the two dataframes

import pandas as pd
merged_df = pd.merge(dataframe_a, dataframe_b, on='name')
azal
  • 1,210
  • 6
  • 23
  • 43
0

You can use reduce from functools, or you can use isin, to create a new_df that only contains values in dfA that are also present in dfB.

Approach 1 using reduce:

from functools import reduce #import package

li = [dfA, dfB] #create list of dataframes
new_df = reduce(lambda left,right: pd.merge(left,right,on='name'), li) #reduce list

Approach 2 using isin:

new_df = dfA[dfA['name'].isin(dfB['name])]
R_Dax
  • 706
  • 3
  • 10
  • 25
0

One way you could do this is to utilise python's set functionality.

This will convert the specified columns to sets and then create a new dataframe using the output.

dataframe = pd.DataFrame(data = {
    'name': list(set(dataframeA['name'].tolist()) - set(dataframeB['name'].tolist()))
})