Find common values in two different dataframes

2

Editing the question:

I have two DataFrames of different sizes, they are:

 df1 = pd.DataFrame({'bc': bc_1}, index=altura_1)    
 df1.shape()=(73,1)
 >>> print df1
             bc
  1.175441  0.002884
  1.115565  0.001905
  1.055689  0.003029
  0.995812  0.003366
  .
  .
  .

 df2 = pd.DataFrame({'bc': bc_2}, index=altura_2)    
 df2.shape()=(18,1)
 >>> print df2
            bc
  0.150  0.000005
  0.165  0.000007
  0.180  0.000010
  .
  .
  .

And they're measuring the same variable, only using two different instruments.

The graph representing this data is as follows:

Do you realize that in some moments, the curves (the points) are very close? I need to figure out what these points are and store them in another dataframe

    
asked by anonymous 27.08.2018 / 18:40

1 answer

2

Starting from these two dataframes:

>>> print df1
       dados
1.1    1.123
2.2    1.567
3.3    2.001
4.4    2.345

>>> print df2
       dados
1.0     1.12
2.3     1.56
3.5     2.00

Calculate the distance of two points on a graph:

def calc_distancia(x1, y1, x2, y2):
    return ((x1-x2)**2 + (y1-y2)**2) ** (1/2.0)

Now, we define a df3 and calculate the distance of the points of df1 and df2. If the distance is less than a value, we add the data in df3:

DISTANCIA_MAXIMA = 0.5#Defini a distância máxima como 0.5

df3 = pd.DataFrame(data={'X1':[], 'Y1':[], 'X2':[], 'Y2':[], 'Distancia':[]})
#Verifia quais valores são iguais comparando todos os dados1 com todos os dados2
for index_df1, row_df1 in df1.iterrows():
    for index_df2, row_df2 in df2.iterrows():
        distancia = calc_distancia(index_df1, row_df1['dados'], index_df2, row_df2['dados'])
        if distancia < DISTANCIA_MAXIMA:
            df3 = df3.append({'X1':index_df1, 'Y1':row_df1['dados'], 'X2':index_df2, 'Y2':row_df2['dados'], 'Distancia':distancia}, ignore_index=True)

>>> print df3
   Distancia   X1   X2     Y1    Y2
0   0.100045  1.1  1.0  1.123  1.12
1   0.100245  2.2  2.3  1.567  1.56
2   0.200002  3.3  3.5  2.001  2.00
    
27.08.2018 / 20:28