BaşlayınÜcretsiz başlayın

Grouping by multiple columns with window functions

Taking the analysis further, you want to break down bike demand by both time of day and weather. Warm sunny hours likely behave differently from cool cloudy ones. Create a simple weather category, then calculate total rentals for each hour-weather combination.

polars is loaded as pl. The DataFrame bikes is available with columns time, rentals, temp, and hour.

Bu egzersiz, kursun bir parçasıdır

Data Transformation with Polars

Kursa Göz Atın

Egzersiz talimatları

  • Create a weather column: "warm" when temp exceeds 20, otherwise "cool".
  • Add total_by_hour_weather with the sum of rentals grouped by hour and weather.

Uygulamalı etkileşimli egzersiz

Bu egzersizi bu örnek kodu tamamlayarak deneyin.

# Add weather category based on temperature
bikes.with_columns(
    pl.when(pl.col("temp") > 20)
    .then(pl.lit("____"))
    .otherwise(pl.lit("____"))
    .alias("weather")
).with_columns(

    # Calculate total rentals by hour and weather
    pl.col("rentals").____().over("____", "____").alias("total_by_hour_weather")
)
Kodu Düzenle ve Çalıştır