1

I'm writing a code to merge several dataframe together using pandas .
Here is my first table :

Index   Values  Intensity   
  1       11      98 
  2       12      855 
  3       13      500
  4       24      140   

and here is the second one:

Index   Values  Intensity
  1       21     1000
  2       11     2000
  3       24     0.55
  4       25     500

With these two df, I concanate and drop_duplicates the Values columns which give me the following df :

Index   Values  Intensity_df1  Intensity_df2
  1       11       0                0
  2       12       0                0
  3       13       0                0
  4       24       0                0
  5       21       0                0
  6       25       0                0

I would like to recover the intensity of each values in each Dataframes, for this purpose, I'm iterating through each line of each df which is very inefficient. Here is the following code I use:

m = 0
while m < len(num_df):
    n = 0
    while n < len(df3):
        temp_intens_abs = df[m]['Intensity'][df3['Values'][n] == df[m]['Values']]
        if temp_intens_abs.empty:
            merged.at[n,"Intensity_df%s" %df[m]] = 0
        else:
            merged.at[n,"Intensity_df%s" %df[m]] = pandas.to_numeric(temp_intens_abs, errors='coerce')
        n = n + 1
    m = m + 1

The resulting df3 looks like this at the end:

Index   Values  Intensity_df1  Intensity_df2
  1       11      98             2000
  2       12     855                0
  3       13     500                0
  4       24     140             0.55
  5       21       0             1000
  6       25       0              500

My question is : Is there a way to directly recover "present" values in a df by comparing directly two columns using pandas? I've tried several solutions using numpy but without success.. Thanks in advance for your help.

1 Answers1

1

You can try joining these dataframes: df3 = df1.merge(df2, on="Values")

Be Chiller Too
  • 2,502
  • 2
  • 16
  • 42