Analyzing Financial Statements in Python
Rohan Chatterjee
Risk modeler

pivot_table to compute the average of the ratios by company: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 to compute the average ratio by industry: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 requires the data to be in a "long" format. Use pd.melt to melt the DataFrames avg_industry_ratio and avg_company_ratio into long format: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 to concatenate molten_plot_company and molten_plot_industrymolten_plot_industry does not have the company DataFrame because it contains the average of the ratios per industry overallpd.concat requires both DataFrames should to have the same columns, so we add the column company to 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()

Analyzing Financial Statements in Python