[Python] Pandas - match column from two df and return value from another columnl

Discussão em 'Python' iniciado por Stack, Novembro 22, 2024.

  1. Stack

    Stack Membro Participativo

    I have a csv of transactions from my bank, and another csv that I have created where I have categorized the most frequently occurring transactions. I want to classify the bank csv with a category by matching the Descriptions between both dataframes. Not all will be classified/have a match.

    df1 ~2.5k rows:

    Description, Amount
    Corner Store, 30
    Cinema, 10
    Trampoline Store, 20


    df2 ~100 rows:

    Description, Category
    Corner Store, Groceries
    Cinema, Recreation
    The Pub, Alcohol


    Want the result:

    Description, Amount, Category
    Corner Store, 30, Groceries
    Cinema, 10, Recreation
    Trampoline Store, 20,


    I have tried this but just get a Category column with 'nan' in my df:

    df1['Category'] = df1['Description'].map(df2.set_index('Description')['Category'])

    Continue reading...

Compartilhe esta Página