शुरू करेंमुफ़्त में शुरू करें

प्लॉट बनाने के लिए user-defined फंक्शन

पिछले अभ्यास में, आपने प्लॉट बनाने के लिए यह कोड लिखा था:

# Subset tech and fmcg companies
subset_dat = dataset.loc[dataset["comp_type"].isin(["tech", "fmcg"])]

# Compute yearly average gross margin ratio of tech and fmcg companies
subset_dat_avg = subset_dat.pivot_table(index=["Year", "comp_type"], values = "gross_margin").reset_index()

# Add column company
subset_dat_avg["company"] = np.where(subset_dat_avg["comp_type"]=="tech", "Avg tech", "Avg fmcg")

# Concat the DataFrames
plot_df = pd.concat([subset_dat, subset_dat_avg], axis=0)

# Make the plot
sns.relplot(data=plot_df.reset_index(drop=True), x="Year", y="gross_margin", hue="company", col="comp_type", kind="line")
plt.show()
plt.close()

ध्यान दें कि इस अभ्यास में हम tech और FMCG DataFrames पर वही क्रियाएँ दोहराते हैं। यह दोहराव है और DRY सिद्धांत — Don't repeat yourself — के खिलाफ जाता है। दोहराव वाला कोड ठीक नहीं होता क्योंकि यह आपका काम बढ़ाता है और आपके कोड में गलती होने की संभावना भी बढ़ाता है। इस अभ्यास में, आप डेटा प्रोसेस करने और फ़िगर्स प्लॉट करने के लिए अपना फंक्शन परिभाषित करेंगे।

यह अभ्यास पाठ्यक्रम का हिस्सा है

Python में Financial Statements का विश्लेषण

पाठ्यक्रम देखें

इंटरैक्टिव व्यावहारिक अभ्यास

हमारे इंटरैक्टिव अभ्यासों में से किसी एक के साथ सिद्धांत को व्यवहार में बदलें

अभ्यास शुरू करें