Lazy पिवट से वाइड फ़ॉर्मेट
डैशबोर्ड टीम को चेकआउट्स का वाइड month-by-format व्यू चाहिए. Polars lazy मोड में .pivot() सपोर्ट करता है जब आप आउटपुट कॉलम नाम पहले से तय कर देते हैं. इससे पूरी पाइपलाइन एंड-टू-एंड optimized रहती है.
LazyFrame monthly_checkouts में long फ़ॉर्मेट में totals हैं, जिनमें month, format, और total कॉलम हैं, और लिस्ट formats में डेटा के हर फ़ॉर्मेट का नाम है.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Polars के साथ Data Pipelines का स्केलिंग और ऑप्टिमाइज़ेशन
अभ्यास निर्देश
monthly_checkoutsको पिवट करें ताकि हरformatअपनी अलग कॉलम बन जाए.formatsलिस्ट पास करें ताकि lazy पिवट को आउटपुट स्कीमा पहले से पता हो.- बने हुए DataFrame को
monthके अनुसार sort करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
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)