Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two data frames with the closest number into a single row using pandas?

I have two data frames:

df1
col1      col2
 8         A
 12        C
 20        D

df2
col1     col3
 7        F
 15       G

I want to merge these two data frames on col1 in a way that the closest value of col1 from df2 and df1 will merge in a single row.

the final data frame will look like,

df
col1    col2    col3
 8        A      F
 12       C      G
 20       D      NA

I can do this using for loop and comparing the numbers, but the execution time will be huge.

Is there any pythonic way to do it, so the runtime will be reduced. Some pandas shortcut may be.

like image 873
Kallol Avatar asked Oct 24 '19 07:10

Kallol


People also ask

How do I merge two data frames in the same row in Python?

The concat() function can be used to concatenate two Dataframes by adding the rows of one to the other. The merge() function is equivalent to the SQL JOIN clause. 'left', 'right' and 'inner' joins are all possible.

How do I merge two DataFrames by index in Pandas?

concat() to Merge Two DataFrames by Index. You can concatenate two DataFrames by using pandas. concat() method by setting axis=1 , and by default, pd. concat is a row-wise outer join.

How do I merge two datasets in Pandas?

Pandas DataFrame merge() function is used to merge two DataFrame objects with a database-style join operation. The joining is performed on columns or indexes. If the joining is done on columns, indexes are ignored. This function returns a new DataFrame and the source DataFrame objects are unchanged.

How do I concatenate two data frames?

We'll pass two dataframes to pd. concat() method in the form of a list and mention in which axis you want to concat, i.e. axis=0 to concat along rows, axis=1 to concat along columns.


1 Answers

Use merge_asof with direction='nearest' and tolerance parameter:

df = pd.merge_asof(df1, df2, on='col1', direction='nearest', tolerance=3)
print (df)
   col1 col2 col3
0     8    A    F
1    12    C    G
2    20    D  NaN
like image 150
jezrael Avatar answered Sep 30 '22 04:09

jezrael