LoslegenKostenlos starten

Creating reusable expressions

Now that you can transform columns, you want to understand which artists have a dedicated fanbase by calculating streams per listener. Rather than rewriting this formula each time, store the calculation as a Polars expression variable that you can reuse across multiple DataFrames.

polars is loaded as pl. The DataFrame spotify is available with album streaming data.

Diese Übung ist Teil des Kurses

<Kurs>Data Transformation with Polars</Kurs>
Kurs ansehen

Übungsanweisungen

  • Create an expression that divides streams by monthly_listeners.
  • Apply the expression variable and sort by streams_per_listener in descending order.

Interaktive praktische Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# Create a reusable expression for streams per listener
streams_per_listener_expr = (
    (pl.col("____") / pl.col("____"))
    .alias("streams_per_listener")
)

# Apply the expression and sort by streams_per_listener
result = spotify.with_columns(____).sort(
    "streams_per_listener", descending=____
)

print(result.head())
Code bearbeiten und ausführen