Pandas: merge data files without creating new columns

I have 2 dataframes with the same columns:

df1 = pd.DataFrame([['Abe','1','True'],['Ben','2','True'],['Charlie','3','True']], columns=['Name','Number','Other'])
df2 = pd.DataFrame([['Derek','4','False'],['Ben','5','False'],['Erik','6','False']], columns=['Name','Number','Other'])

which give:

     Name Number Other
0      Abe      1  True
1      Ben      2  True
2  Charlie      3  True

and

    Name Number  Other
0  Derek      4  False
1    Ben      5  False
2   Erik      6  False

I want the output data block to be the intersection of two based on "Name":

output_df = 
        Name Number  Other
    0    Ben      2  True
    1    Ben      5  False

I tried the basic pandas merge but return is not advisable:

pd.merge(df1,df2,how='inner',on='Name') = 
 Name Number_x Other_x Number_y Other_y
0  Ben        2    True        5   False

These data files are quite large, so I prefer to use pandas magic so that everything is in order.

+4
source share
1 answer

You can use concatand then filter by isinwith numpy.intersect1dwith boolean indexing:

val = np.intersect1d(df1.Name, df2.Name)
print (val)
['Ben']

df = pd.concat([df1,df2], ignore_index=True)
print (df[df.Name.isin(val)])
  Name Number  Other
1  Ben      2   True
4  Ben      5  False

Another possible solution for valis intersectionsets:

val = set(df1.Name).intersection(set(df2.Name))
print (val)
{'Ben'}

Then it is possible to reset index to monotonic:

df = pd.concat([df1,df2])
print (df[df.Name.isin(val)].reset_index(drop=True))
  Name Number  Other
0  Ben      2   True
1  Ben      5  False
+6
source

Source: https://habr.com/ru/post/1664420/


All Articles