[Python] Pandas smarter conditional merge for two dataframes

Discussão em 'Python' iniciado por Stack, Março 9, 2025.

  1. Stack

    Stack Membro Participativo

    Here I tried to find the shortest possible example code for a conditional merge of two dataframes. There I try to merge exact rows based on a condition. Currently I have to iterate over both dataframes for that.

    import pandas as pd

    df1 = pd.DataFrame([
    ['a', 'Test1', 2995],
    ['b', 'Test2', 3995],
    ['c', 'Test3', 5000]],
    columns=['ID', 'Name', 'Value'])
    df2 = pd.DataFrame([
    ['1', 1, 2],
    ['2', 3, 4],
    ['3', 99, 100]],
    columns=['ID', 'Start', 'Context'])

    df_test = pd.DataFrame(columns=['ID', 'Name', 'Value','Start'])
    i = 0
    for index, row1 in df1.iterrows():
    for index, row2 in df2.iterrows():
    if row1['Value'] + 25 > row2['Start'] * 1000 and row1['Value']< row2['Start'] * 1000:
    df_test.loc = row1
    df_test.loc['Start'] = row2['Start']
    i += 1


    And it works so the dft_test is

    ID Name Value Start
    0 a Test1 2995 3


    Is there a smarter way doing that?

    Continue reading...

Compartilhe esta Página