[Python] Create a new column of dictionaries where keys are in another column of lists, and...

Discussão em 'Python' iniciado por Stack, Fevereiro 12, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to create the "Related Quantities" column of a dataframe given the existing "Item", "Quantity", and "Related Items" columns.

    Item Quantity Related Items Related Quantities
    0 Flowers 1 ['Bushes'] {'Bushes': 2}
    1 Bushes 2 ['Flowers'] {'Flowers': 1}
    2 Cars 3 ['Trucks', 'Motorcycles'] {'Trucks': 4, 'Motorcycles': 5}
    3 Trucks 4 ['Cars', 'Motorcycles'] {'Cars': 3, 'Motorcycles': 5}
    4 Motorcycles 5 ['Cars', 'Trucks'] {'Cars': 3, 'Trucks': 4}

    The values in the dictionaries will be used as the input of a function to generate another column later


    I believe I have a line that will make the dictionary for a single row, which I could use to fill out the column using iterrows() and a for loop:

    dictionary = {item : df.loc[df['Item'] == item,['Quantity']][idx] for item in related_items_list}


    (where idx is something to grab the corresponding index of the one row left after filtering by 'Item', and related_items_list is the value grabbed from the 'Related Items' column of the current row in the loop)

    But I'm trying to make something with df.apply() instead, in hopes that it will be more performant. Is there some way to allow the function called in apply() to access the whole dataframe instead of just the row passed to it?

    I think I may be way overcomplicating this. Is trying to use apply() for performance a waste of time? Should I just be using a loop to feed the 'Quantity' data into the function directly instead of making this column at all? I worry that will also hurt performance

    Continue reading...

Compartilhe esta Página