ComenzarEmpieza gratis

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.

Este ejercicio forma parte del curso

Introduction to Polars

Ver curso

Instrucciones del ejercicio

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

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

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)
Editar y ejecutar código