LoslegenKostenlos loslegen

Pivoting a DataFrame

You want to convert the Spotify data from long format to wide format in order to compare the number of streams for each artist in each year.

The spotify_df DataFrame is available for you and has been modified to get the number of streams for each artist in each year in artist_year_df.

Diese Übung ist Teil des Kurses

Introduction to Polars

Kurs anzeigen

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

artist_year_df = spotify_df.group_by("artist", "year").agg(pl.col("streams").sum())

pivoted_df = (
    artist_year_df
    # Pivot with artists as rows, years as columns and streams as values
    .pivot(
        on=____,
        index=____,
        values=____
    )
)

print(pivoted_df)
Code bearbeiten und ausführen