[Python] Shortcut for assigning variables to row names within df.iterrows()

Discussão em 'Python' iniciado por Stack, Maio 22, 2025.

  1. Stack

    Stack Membro Participativo

    Instead of using the below code, is there a method which can produce n_apple, n_cherry, and n_banana for the current row in a single line?

    for i, row in df.iterrows():
    date = row['date']
    n_apple = row['apple']
    n_cherry = row['cherry']
    n_banana = row['banana']
    # the list goes on...



    Edit for clarity: My actual column names are much longer, so this won't work

    n_apple, n_cherry, n_banana = row['apple'], row['cherry'], row['banana']


    EDIT 2: Desired behavior:

    n_apple, n_cherry, n_banana = row._unknown_method(['apple', 'cherry', 'banana'])

    Continue reading...

Compartilhe esta Página