Aggregated values दिखाएँ
कभी-कभी आपको अपने डेटा को अधिक aggregated रूप में दिखाने की ज़रूरत पड़ती है. उदाहरण के लिए, co2_levels डेटा साप्ताहिक है, लेकिन आपको इसके मानों को वर्ष के महीनों के आधार पर aggregate करके दिखाना पड़ सकता है. ऐसे डेटासेट्स में जैसे co2_levels DataFrame, जहाँ इंडेक्स datetime टाइप का होता है, आप इंडेक्स में मौजूद तिथियों का year निकाल सकते हैं:
# df DataFrame की प्रत्येक तिथि का year निकालें
index_year = df.index.year
df DataFrame के इंडेक्स में तिथियों का month या day निकालने के लिए क्रमशः df.index.month और df.index.day का उपयोग करें.
इसके बाद आप co2_levels DataFrame के प्रत्येक इंडेक्स का निकाला गया year और groupby फंक्शन का उपयोग करके वर्ष के हिसाब से औसत CO2 स्तर निकाल सकते हैं:
df_by_year = df.groupby(index_year).mean()
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Time Series Data का विज़ुअलाइज़ेशन
अभ्यास निर्देश
co2_levelsDataFrame के इंडेक्स में मौजूद प्रत्येक तिथि का month निकालें और उसेindex_monthनाम के वैरिएबल में असाइन करें.pandasलाइब्रेरी केgroupbyऔरmeanफंक्शनों का उपयोग करकेco2_levelsDataFrame में मासिक औसत CO2 स्तर निकालें और उसेmean_co2_levels_by_monthनाम के नए DataFrame में असाइन करें.mean_co2_levels_by_monthDataFrame के मानों का प्लॉट बनाएँ और axis ticks के लिए fontsize 6 रखें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Get month for each dates in the index of co2_levels
index_month = ____.index.____
# Compute the mean CO2 levels for each month of the year
mean_co2_levels_by_month = co2_levels.____(____).____()
# Plot the mean CO2 levels for each month of the year
mean_co2_levels_by_month.____
# Specify the fontsize on the legend
plt.legend(fontsize=10)
# Show plot
plt.show()