I am trying to re-arrange a DataFrame that I automatically read in from a json using Pandas. I\'ve searched but have had no success.
I have the following json (saved
Your first attempt was nearly correct, just use columns='value_id'
instead of including it in the index.
# Perform the pivot.
df = df.pivot_table(
values='value',
index=['stream_name', 'preferred_timestamp', 'internal_timestamp'],
columns='value_id'
)
# Formatting.
df.reset_index(inplace=True)
df.columns.name = None
This isn't an issue in your example data, but keep in mind that pivot_table
will aggregate values if multiple values are pivoted to the same position (taking the mean by default).