सबसे लोकप्रिय गाने
आपके पास इस Spotify डेटा पर एक और टास्क है: सभी उपलब्ध वर्षों में से टॉप 10 सबसे लोकप्रिय गाने ढूँढना. इसके लिए आपको यह एल्गोरिदम इस्तेमाल करना होगा: हर साल के टॉप 10 गाने निकालें, फिर इन्हें मिलाकर इन सभी में से टॉप 10 ढूँढें.
नीचे दिया गया फंक्शन, जो किसी DataFrame में टॉप 10 गाने ढूँढता है, आपके लिए उपलब्ध है और आपके environment में लोड है.
def top_10_most_popular(df):
return df.nlargest(n=10, columns='popularity')
dask और delayed() फंक्शन आपके लिए इम्पोर्ट किए गए हैं. pandas को pd के रूप में इम्पोर्ट किया गया है. फाइलनाम की list आपके environment में filenames नाम से उपलब्ध है, और हर फाइल का year years list में स्टोर है.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Dask के साथ Parallel Programming
अभ्यास निर्देश
- हर फाइल के लिए,
top_10_most_popular()फंक्शन का उपयोग करके उस साल के टॉप 10 गाने निकालें. - हर साल की टॉप 10 वाली list को compute करें, और प्राप्त tuple के पहले आइटम का चयन करें.
- concatenated DataFrame पर टॉप 10 गाने ढूँढने के लिए
top_10_most_popular()फंक्शन चलाएँ.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
top_songs = []
for file in filenames:
df = delayed(pd.read_csv)(file)
# Find the top 10 most popular songs in this file
df_top_10 = ____
top_songs.append(df_top_10)
# Compute the list of top 10s
top_songs_list = ____
# Concatenate them and find the best of the best
top_songs_df = pd.concat(top_songs_list)
df_all_time_top_10 = ____
print(df_all_time_top_10)