Phân tích báo cáo tài chính bằng Python
Rohan Chatterjee
Risk modeler

pivot_table để tính trung bình tỷ số theo công ty: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 để tính trung bình tỷ số theo ngành: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 cần dữ liệu ở dạng “dài”. Dùng pd.melt để chuyển avg_industry_ratio và avg_company_ratio sang dạng dài: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 để nối molten_plot_company và molten_plot_industrymolten_plot_industry không có cột company vì chứa trung bình tỷ số theo toàn ngànhpd.concat yêu cầu hai DataFrame có cùng cột, nên thêm cột company vào 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()

Phân tích báo cáo tài chính bằng Python