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.
Diese Übung ist Teil des Kurses
Introduction to Polars
Anleitung zur Übung
- 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".
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
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)