Aan de slagGa gratis aan de slag

Converting streams to billions

You are a data analyst at a record company. You have a Spotify dataset containing 25 popular albums with streaming metrics such as total plays, monthly listeners, and popularity scores. The streams column shows total plays as large integers that are hard to read, so you need to convert them to billions using a custom Python function.

polars is loaded as pl. The DataFrame spotify and a to_billions() function are preloaded for you.

Deze oefening maakt deel uit van de cursus

Data Transformation with Polars

Cursus bekijken

Oefeninstructies

  • Apply to_billions to the streams column to create a new column called streams_billions.
  • Specify the return type as pl.Float64 for explicit type control.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Apply to_billions to the streams column
result = spotify.with_columns(
    pl.col("streams").____(
        ____, return_dtype=____
    ).alias("streams_billions")
)

print(result.head())
Code bewerken en uitvoeren