0

I have a Pandas data frame:

timestamp   device_id   metric_name     metric_value
2020-10-20  6C0301      throughput      5.0
2020-10-21  6C0301      throughput      6.3
2020-10-20  6C0301      cache           4.7
2020-10-21  6C0301      cache           2.1
2020-10-20  6C0302      throughput      1.4
2020-10-21  6C0302      throughput      1.8
2020-10-22  6C0302      blocks          9.3
2020-10-23  6C0302      blocks          7.2

So different devices at different times, each with their own set of metric_names and metric_values.

I need to produce a table that looks like this:

timestamp   device_id   throughput   cache   blocks   
2020-10-20  6C0301      5.0          4.7     NULL
2020-10-21  6C0301      6.3          2.1     NULL
2020-10-20  6C0302      1.4          NULL    NULL
2020-10-21  6C0302      1.8          NULL    NULL
2020-10-22  6C0302      NULL         NULL    9.3
2020-10-23  6C0302      NULL         NULL    7.2

So the values in the metric_name column gets "unmelted" into its own individual columns.

I am looking at Pandas pivot_table but it gives me wrong result:

pd.pivot_table(df, values='metric_name', index=['timestamp', 'device_id'], columns=['metric_value'])

Tells me "DataError: No numeric types to aggregate" even though the metric_value column is float, and I am not looking to aggregate anything.

Cybernetic
  • 12,628
  • 16
  • 93
  • 132

0 Answers0