Add column to a dataframe based on the classification made on another dataframe

Asked

Viewed 6,608 times

1

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

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

1 answer

1

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

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
  • thanks! I got it here, thank you very much! abs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.