Unpivoting Data from Wide to Long Format
You've done a groupby-aggregation on the Spotify data that gives you a range of statistics on the number of streams per artist per year in the DataFrame wide_df. You now want to convert the data in wide_df to long format for use in a data visualization.
Cet exercice fait partie du cours
Introduction to Polars
Instructions
- Use the
"artist"and"year"as the index columns to identify the data on each row. - Unpivot on the statistics columns in
wide_df:"avg_streams","max_streams","min_streams". - Name the variable column as
"metric"and the value column as"stream_count".
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
long_df = (
wide_df
.unpivot(
# Set the index columns
index=____,
# Unpivot on the stats columns
on=____,
# Name the variable and value columns
variable_name=____,
value_name=____
)
)
print("\nLong format DataFrame:")
print(long_df)