MulaiMulai sekarang secara gratis

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.

Latihan ini adalah bagian dari kursus

Introduction to Polars

Lihat Kursus

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

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)
Edit dan Jalankan Kode