Pythonで学ぶ財務諸表分析
Rohan Chatterjee
Risk Modeler
pandas の DataFrame balance_sheet に読み込み済み。
print(balance_sheet.head())

balance_sheet["current_ratio"] = balance_sheet["Total Current Assets"] /
balance_sheet["Total Current Liabilities"]
print(balance_sheet.head())

balance_sheet.groupby("comp_type")["current_ratio"].mean()

balance_sheet.groupby(["Year","comp_type"])["current_ratio"].mean()

.groupby() の後に .transform() を使うと、各行の属するグループの結果を列として付与できます。balance_sheet["industry_curr_ratio"] =
balance_sheet.groupby([
"Year","comp_type"])["current_ratio"].transform("mean")
print(balance_sheet.head())

balance_sheet["relative_diff"] =
(balance_sheet["current_ratio"] /
balance_sheet["industry_curr_ratio"]) - 1

.isin() は分析用のデータ抽出に使います。fmcg と tech 企業のみを抽出:fmcg_2019 = balance_sheet.loc[
(balance_sheet["Year"].isin([2019,2020])) &
(balance_sheet["comp_type"].isin(["tech","fmcg"]))
]
Pythonで学ぶ財務諸表分析