[Python] Convert column headers into a single column with pandas

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

  1. Stack

    Stack Membro Participativo

    i want to convert the elements into a single column. i have tried pivot and pivot table and because i have duplicates it doesnt allow, i have also tried melting

    University id # Year financial_period element #1 element #2 element #3
    1 2022 prior_year $1 $3 $2
    1 2022 current_year $2 $5 $0

    This is what I'm trying to get

    University id # Year fin_element prior_year current_year
    1 2022 element #1 $1 $2
    1 2022 element #2 $3 $5
    1 2022 element #3 $2 $0

    i have tried

    df_melt = df.melt(id_vars=df.columns[:2], value_vars='element')


    which moves the elements into columns, but i can't figure out how to turn financial period into column headers

    Continue reading...

Compartilhe esta Página