[Python] Trim Datasets in Multiindex Pandas

Discussão em 'Python' iniciado por Stack, Abril 15, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dataset that is structured as a multiindex in pandas, with first index "Date" and second index "TagId". So I would access the data by using: df.loc['01/06/2022'].loc['0x24025F44AD21'][...]

    Here is a snippet of what that looks like:

    X Y Time
    Date TagId
    01/06/2022 0x24025F44AD21 3.121672 5.188564 1900-01-01 08:37:17.000
    0x24025F44AD21 3.121672 5.188564 1900-01-01 08:37:17.200
    0x24025F44AD21 3.121672 5.188564 1900-01-01 08:37:17.400
    0x24025F44AD21 3.121672 5.188564 1900-01-01 08:37:17.600
    0x24025F44AD21 3.121672 5.188564 1900-01-01 08:37:17.800
    ... ... ...
    0x24025F44AD21 [......] [......] 1900-01-01 11:59:26.400

    0x24046130B076 [......] [......] 1900-01-01 08:58:10:200
    ... ... ...
    0x24046130B076 7.611438 2.346377 1900-01-01 12:31:05.800
    0x24046130B076 7.611438 2.346377 1900-01-01 12:31:06.000
    0x24046130B076 7.533170 2.035381 1900-01-01 12:31:06.200
    0x24046130B076 7.533170 2.035381 1900-01-01 12:31:06.400


    I want an easy and efficient way to make sure all the first and last times (from the "Time" Columns) lign up. So, if you will, I want to find the latest first time and the earliest last time so that I can trim the other datasets accordingly.

    Additionally, which you can't see in the data above, there is a gap in the data. A break of sorts, where no measurements were taken. I want those "breaks" to align as well.

    So my goal is that the output of the above dataset looks something like this:

    X Y Time
    Date TagId
    01/06/2022 0x24025F44AD21 [......] [......] 1900-01-01 08:58:10:200
    ... ... ...
    0x24025F44AD21 [......] [......] 1900-01-01 11:59:26.400

    0x24046130B076 [......] [......] 1900-01-01 08:58:10:200
    ... ... ...
    0x24025F44AD21 [......] [......] 1900-01-01 11:59:26.400


    (Here I used [......] because I don't know what these values are. It is mainly about the time column)

    It would be great if somebody could help me. Thanks in advance!

    Continue reading...

Compartilhe esta Página