Add column to a dataframe based on sorting done in another dataframe

1

Greetings! I have two dataframes. Both have columns that determine ranges, with maximum and minimum numbers. In each dataframe the ranking of each range is done one way. I wanted to make a new column in the second dataframe with the rating according to the rating made in the first one.

For example:

import pandas as pd
import numpy as np
var1 = 'df1.csv'
var2 = 'df2.csv'
df1 = pd.read_csv(var1, sep=';')
df2 = pd.read_csv(var2, sep=';')

print(df1)


Topo    Base    CLASSIFICADOR_1
0       10      A
11      22      B 
23      37      C

print(df2)

Topo    Base    ClASSIFICADOR_2
0       2       T
3       5       N
6       10      T
11      14      P
15      22      U
23      33      N
34      37      P

The result I'd like is basically this:

Topo    Base    CLASSIFICADOR_2  CLASSIFICADOR_1
0       2       T                A
3       5       N                A
6       10      T                A
11      14      P                B
15      22      U                B
23      33      N                C
34      37      P                C
    
asked by anonymous 31.01.2018 / 01:52

1 answer

1

The answer I developed is "pythonica" and "pandonica", but that's what I got. I hope it helps!

import pandas as pd

df1 = pd.read_csv('df1.csv')
df2 = pd.read_csv('df2.csv')

df1_values = df1.values
df2_values = df2.values

# lista com o valor a ser apendado futuramente ao df2
classificador_1 = []

for line2 in df2_values:
    topo2 = line2[0]
    base2 = line2[1]

    for line1 in df1_values:
        topo1 = line1[0]
        base1 = line1[1]

        # condicao de uniao de tabelas
        if (topo2 >= topo1 and base2 <= base1):
            classificador_1.append(line1[2])

    df2['CLASSIFICADOR_1'] = classificador_1
    
31.01.2018 / 05:29