regretoverflow
regretoverflow

Reputation: 2153

Pivot DataFrame into a timeseries

I have a DataFrame that looks like:

parent | 07-Sep-20 08:31:10 | 07-Sep-20 12:31:10 | 07-Sep-20 16:31:10 | 07-Sep-20 20:31:10 | 08-Sep-20 00:31:10 |
=================================================================================================================
001    | 36.45              | 38                 | 37.5               | 39                 | 42
023    | 8.5                | 7                  | 9.25               | 9.75               | 7.55

that I need to pivot into a timeseries, like:

parent |     timestamp      | value
===================================
001    | 07-Sep-20 08:31:10 | 36.45
001    | 07-Sep-20 12:31:10 | 38
001    | 07-Sep-20 16:31:10 | 37.5
001    | 07-Sep-20 20:31:10 | 39
001    | 08-Sep-20 00:31:10 | 42
023    | 07-Sep-20 08:31:10 | 8.5
023    | 07-Sep-20 12:31:10 | 7
023    | 07-Sep-20 16:31:10 | 9.25
023    | 07-Sep-20 20:31:10 | 9.75
023    | 08-Sep-20 00:31:10 | 7.55

So, I assume parent is my index, and then I need to pivot columns 1:5 into a column called value, and add an attribute named timestamp that is the header name of the pivoted column. Using Jupyter, I've tried every manner of df.pivot_table, but can't get my head wrapped around the syntax, and all the examples I've found are doing the OPPOSITE of what I'm trying to do. The sticky part is that I don't know ahead of time what the date columns will be named, I only know they will be index 1-5. The output of this operation is not used for anything in pandas, I need to save it back into a CSV for use in Tableau, pandas is just my ETL tool for this operation.

Upvotes: 1

Views: 37

Answers (1)

BENY
BENY

Reputation: 323376

This is more like melt not pivot

df_out = df.melt('parent')

Upvotes: 3

Related Questions