[Python] How do i create code for a vlookup in python?

Discussão em 'Python' iniciado por Stack, Junho 23, 2025.

  1. Stack

    Stack Membro Participativo

    df

    Season Date Team Team_Season_Code TS L Opponent Opponent_Season_Code OS
    2019 20181109 Abilene_Chr 1_2019 94 Home Arkansas_St 15_2019 73
    2019 20181115 Abilene_Chr 1_2019 67 Away Denver 82_2019 61
    2019 20181122 Abilene_Chr 1_2019 72 N Elon 70_2019 56
    2019 20181123 Abilene_Chr 1_2019 73 Away Pacific 224_2019 71
    2019 20181124 Abilene_Chr 1_2019 60 N UC_Riverside 306_2019 48

    Overall_Season_Avg

    Team_Season_Code Team TS OS MOV
    15_2019 Arkansas_St 70.909091 65.242424 5.666667
    70_2019 Elon 73.636364 71.818182 1.818182
    82_2019 Denver 74.03125 72.15625 1.875
    224_2019 Pacific 78.333333 76.466667 1.866667
    306_2019 UC_Riverside 79.545455 78.060606 1.484848

    I have these two dataframes and I want to be able to look up the Opponent_Season_Code from df in Overall_Season_Avg - "Team_Season_Code" and bring back "TS" and "OS" to create a new column in df called "OOS" and "OTS" So a new column for row 1 in df should have Column name OOS with data - 65.24... and Column name OTS with data 70.90...

    In excel its a simple vlookup but i haven't been able to use the solutions that i have found to the vlookup question on overflow so i decided to post my own question. I will also say that the Overall_Season_Avg dataframe was created through by Overall_Season_Avg = df.groupby(['Team_Season_Code', 'Team']).agg({'TS': np.mean, 'OS': np.mean, 'MOV': np.mean})

    Continue reading...

Compartilhe esta Página