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

Lazy pivot to wide format

The dashboard team wants a wide month-by-format view of checkouts. Polars supports .pivot() in lazy mode when you specify the output column names in advance, which keeps the whole pipeline optimized end to end.

The LazyFrame monthly_checkouts holds totals in long format with a month, format, and total column, and the list formats holds every format name in the data.

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

Scaling and Optimizing Data Pipelines with Polars

Kursa Göz Atın

Egzersiz talimatları

  • Pivot monthly_checkouts so each format becomes its own column.
  • Pass the formats list so the lazy pivot knows the output schema in advance.
  • Sort the resulting DataFrame by month.

Uygulamalı etkileşimli egzersiz

Bu egzersizi bu örnek kodu tamamlayarak deneyin.

result = (
    monthly_checkouts
    # Pivot the format column into separate columns
    .pivot(
        on="____",
        index="month",
        values="total",
        # Pre-declare the output column names so the pivot stays lazy
        on_columns=____,
    )
    .collect()
    # Sort the wide result by month
    .sort("____")
)
print(result)
Kodu Düzenle ve Çalıştır