[Python] Expanding/Modifying a data frame by adding data points based on difference between...

Discussão em 'Python' iniciado por Stack, Dezembro 21, 2024.

  1. Stack

    Stack Membro Participativo

    I have a data frame, which is the annotation of animal behavior recording frame by frame. But due to the limitation of the annotation software, the exported data frame is like this:

    Frame stop Behavior
    Frame start
    0 2 turning
    2 5 immobility
    5 9 mild movement
    9 11 immobility
    16 20 jump


    Code:

    data = {
    'Frame start': [0, 2, 5, 9, 16],
    'Frame stop': [2, 5, 9, 11, 20],
    'Behavior': ['turning', 'immobility', 'mild movement', 'immobility', 'jump']}

    df = pd.DataFrame(data)
    df.set_index('Frame start', inplace=True)


    I want to modify the data frame to be like this:

    Frame start
    0 turning
    1 turning
    2 immobility
    3 immobility
    4 immobility
    5 mild movement
    6 mild movement
    7 mild movement
    8 mild movement
    9 immobility
    10 immobility
    11 NaN
    12 NaN
    13 NaN
    14 NaN
    15 NaN
    16 jump
    17 jump
    18 jump
    19 jump


    How can I do that?

    Since I did not annotate every frame of a video, there are gaps of frames with no values, i.e. between index 4 and 5, frame 11-15 in this example. Therefore, I cannot re-index the data frame using the "frame start" column and simply <ffill> to fill up the missing values, which will look like the following:

    new_index = [*range(0, 20)]
    new_df = df.reindex(new_index, method='ffill')
    final_df = new_df.loc[:, 'Behavior']

    print(final_df)

    Frame start
    0 turning
    1 turning
    2 immobility
    3 immobility
    4 immobility
    5 mild movement
    6 mild movement
    7 mild movement
    8 mild movement
    9 immobility
    10 immobility
    11 immobility
    12 immobility
    13 immobility
    14 immobility
    15 immobility
    16 jump
    17 jump
    18 jump
    19 jump

    Continue reading...

Compartilhe esta Página