Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas: Round integers before joining dataframes

I have two data frames that both contain coordinates. One of them, df1, has coordinates at a better resolution (with decimals), and I would like to join it to df2 which has a less-good resolution:

import pandas as pd

df1 = pd.DataFrame({'x': [1.1, 2.2, 3.3],
                    'y': [2.3, 3.3, 4.1],
                    'val': [10,11,12]})

df2 = pd.DataFrame({'x': [1,2,3,5.5],
                    'y': [2,3,4,5.6]})

df1['x_org']=df1['x']
df1['y_org']=df1['y']
df1[['x','y']] = df1[['x','y']].round()
df1 = pd.merge(df1, df2, how='left', on=['x','y'])

df1.drop({'x','y'}, axis=1)
# rename...

The code above does exactly what I want, but it is a bit cumbersome. Is there an easier way to achieve this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use:

df1.merge(df2, 
          how='left', 
          left_on=[df1['x'].round(), df1['y'].round()], 
          right_on=['x','y'], 
          suffixes=('','_')).drop(['x_','y_'], axis=1)

Also is possible remove columns ending by _ dynamic:

df = df1.merge(df2, 
               how='left', 
               left_on=[df1['x'].round(), df1['y'].round()], 
               right_on=['x','y'], 
               suffixes=('','_')).filter(regex='.*[^_]$')

print (df)
     x    y  val
0  1.1  2.3   10
1  2.2  3.3   11
2  3.3  4.1   12

df = df1.merge(df2, 
               how='left', 
               left_on=[df1['x'].round(), df1['y'].round()], 
               right_on=['x','y'], 
               suffixes=('','_end')).filter(regex='.*(?<!_end)$')

print (df)
     x    y  val
0  1.1  2.3   10
1  2.2  3.3   11
2  3.3  4.1   12

Or:

df = (df1.set_index(['x','y'], drop=False).rename(lambda x: round(x))
         .merge(df2.set_index(['x','y']), 
                left_index=True,
                right_index=True, 
                how='left').reset_index(drop=True))
print (df)
     x    y  val
0  1.1  2.3   10
1  2.2  3.3   11
2  3.3  4.1   12
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading