[Python] Using a column value to find the Column header name in Pandas

Discussão em 'Python' iniciado por Stack, Setembro 5, 2025.

  1. Stack

    Stack Membro Participativo

    Scenario: I have a pandas dataframe. I am trying to use the values in a given column (year) to find the relevant header name and add it to a new column (year_name). For example, if the dataframe looks like this:

    itemName 2020 2021 2022 2023 2024 year
    item1 5 20 10 10 50 3
    item2 10 10 50 20 40 2
    item3 12 35 73 10 54 4

    The result should be like this:

    itemName 2020 2021 2022 2023 2024 year year_name
    item1 5 20 10 10 50 3 2022
    item2 10 10 50 20 40 2 2021
    item3 12 35 73 10 54 4 2023

    Obs. the itemName column is the index.

    Issue: I am trying to use a lambda function to use the value of each row of "year" and use it to find the column name for that row and add it to the year_name column.

    Function: I tried:

    col_names = result_dict[col].columns.tolist()
    result_df[[last_year_header']] = result_df[[_last_year']].apply(lambda x: col_names[x])


    but this gave me the following error:

    TypeError: list indices must be integers or slices, not Series


    I also tried:

    col_names = result_dict[col].columns.tolist()
    result_df[[last_year_header']] = result_df[[_last_year']].apply(lambda x: col_names[x.iloc[0].astype(int)])


    But this gave me:

    IndexError: list index out of range


    Question: I am clearly missing something with the implementation of the lambda function in this case. How can I fix this?

    Continue reading...

Compartilhe esta Página