Home > Software engineering >  Convert Time Series Data By Column Into Rows
Convert Time Series Data By Column Into Rows

Time:01-25

I have output from a system which has multiple readings for a date range, date is one column and then each reading is a column of its own, an example data frame looks like this:

 Date/Time        DEVICE_1    DEVICE_2
 01/01  01:00:00  10.141667   8.807851

I would like to convert this into the following format where each column is "flattened" into a row so the output would look something like:

Date/Time        Name     Value    
01/01  01:00:00  DEVICE_1 10.141667
01/01  01:00:00  DEVICE_2 8.807851

If there were ten devices then for each row in the current file for a particular timestamp I would need to extract this into ten rows, one for each device with the same timestamp.

Is this possible with pandas? I don't want to resort to lots of looping if possible.

CodePudding user response:

Using df.melt() method

df = df.melt(id_vars=["Date/Time"], var_name="Name", value_name="Value")
print(df)

        Date/Time      Name      Value
0  01/01 01:00:00  DEVICE_1  10.141667
1  01/01 01:00:00  DEVICE_2   8.807851
  • Related