การวิเคราะห์งบการเงินด้วย Python
Rohan Chatterjee
Risk modeler

pivot_table เพื่อคำนวณค่าเฉลี่ยของอัตราส่วนรายบริษัท:avg_company_ratio = plot_dat.pivot_table(index=["comp_type",
"company"],
values=["Gross Margin", "Operating Margin",
"Debt-to-equity", "Equity Multiplier"],
aggfunc="mean").reset_index()
print(avg_company_ratio.head())
pivot_table เพื่อคำนวณค่าเฉลี่ยของอัตราส่วนรายอุตสาหกรรม:avg_industry_ratio = plot_dat.pivot_table(index="comp_type",
values=["Gross Margin", "Operating Margin",
"Debt-to-equity",
"Equity Multiplier"],
aggfunc="mean").reset_index()
print(avg_industry_ratio.head())
seaborn ต้องการข้อมูลในรูปแบบ "long" ใช้ pd.melt เพื่อแปลง DataFrame avg_industry_ratio และ avg_company_ratio ให้อยู่ในรูปแบบ long:molten_plot_company = pd.melt(avg_company_ratio, id_vars=["comp_type",
"company"])
molten_plot_industry = pd.melt(avg_industry_ratio,
id_vars=["comp_type"])
print(molten_plot_company.head())
print(molten_plot_industry.head())
pd.concat เพื่อรวม molten_plot_company และ molten_plot_industry เข้าด้วยกันmolten_plot_industry ไม่มีคอลัมน์ company เนื่องจากมีเพียงค่าเฉลี่ยของอัตราส่วนรายอุตสาหกรรมโดยรวมpd.concat ต้องการให้ทั้งสอง DataFrame มีคอลัมน์เหมือนกัน จึงต้องเพิ่มคอลัมน์ company ให้ molten_plot_industrymolten_plot_industry["company"] = "Industry Average"
molten_plot = pd.concat([molten_plot_company, molten_plot_industry])
sns.barplot(data=molten_plot, y="variable", x="value", hue="company", ci=None)
plt.xlabel(""), plt.ylabel("")
plt.show()

การวิเคราะห์งบการเงินด้วย Python