[Python] Python - Reformatting a dataframe with repeated columns into separate rows

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

  1. Stack

    Stack Membro Participativo

    let's say I have a DataFrame of the following format:

    Raw data

    In my actual data, columns 1 to 21 are descriptors of the entire row of data (e.g. details of a person). From there on the data is repeated in intervals of 25 columns, where each of these sets of columns repeats the same information but for separate events (e.g. each section of 25 columns could be 25 individual physical test results performed on some day). I would like to have every separate event as a row, and for each data to sit in the same columns as event 1, and to take on the descriptors from columns 1 to 21 of the initial row of data that it came from. Hope that makes sense.

    What is the best way to reformat it into the following: How I want my data

    I have tried to do this with an entire script but it has gotten messy and extremely inefficient.

    I was able to get one column from each event into a new DataFrame using pd.meld, but couldn't figure out how to bring every other column with it.

    Continue reading...

Compartilhe esta Página