Get startedGet started for free

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.

This exercise is part of the course

Introduction to Polars

View Course

Exercise 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".

Hands-on interactive exercise

Have a go at this exercise by completing this sample 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)
Edit and Run Code