การคำนวณอัตราส่วนทางการเงินด้วย pandas

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

Rohan Chatterjee

Risk Modeler

โครงสร้างของข้อมูลงบดุล

  • ข้อมูลงบดุลโหลดไว้ใน DataFrame ของ pandas ชื่อ balance_sheet

 


----CODE_GLUE----
```print(balance_sheet.head())

----CODE_GLUE---- ` ภาพแสดงแถวแรกของ DataFrame ชื่อ balance_sheet

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

การคำนวณ current ratio

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

ภาพแสดง DataFrame balance_sheet ที่มีคอลัมน์ current_ratio เพิ่มเข้ามา

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

ใช้ .groupby() เพื่อดูผลลัพธ์รายกลุ่ม

  • เพื่อหาค่าเฉลี่ย current ratio รายอุตสาหกรรม:
    balance_sheet.groupby("comp_type")["current_ratio"].mean()
    
    ภาพแสดงค่าเฉลี่ย current ratio ของแต่ละอุตสาหกรรม
การวิเคราะห์งบการเงินด้วย Python

ใช้ .groupby() เพื่อดูผลลัพธ์รายกลุ่ม

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

ภาพแสดงค่าเฉลี่ย current ratio รายอุตสาหกรรมในแต่ละปี

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

การใช้ groupby().transform()

  • .transform() ใช้ต่อจาก .groupby() เพื่อนำผลลัพธ์ของ groupby กลับไปแนบกับแต่ละแถวตามกลุ่มที่แถวนั้นสังกัด
    balance_sheet["industry_curr_ratio"] = 
              balance_sheet.groupby([
              "Year","comp_type"])["current_ratio"].transform("mean")
    print(balance_sheet.head())
    

ภาพแสดงค่าเฉลี่ย current ratio รายอุตสาหกรรมในแต่ละปี ที่ถูกแนบกลับเข้า DataFrame balance_sheet แล้ว

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

การใช้ .groupby().transform()

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

DataFrame แสดงความแตกต่างเชิงสัมพัทธ์ระหว่าง current ratio ของบริษัทกับค่าเฉลี่ยของอุตสาหกรรม

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

การใช้ .isin()

  • .isin() ใช้กรองข้อมูลสำหรับการวิเคราะห์
  • ตัวอย่าง: กรอง DataFrame เพื่อแสดงบริษัท fmcg และ tech ในปี 2019 และ 2020:
fmcg_2019 = balance_sheet.loc[
            (balance_sheet["Year"].isin([2019,2020])) &
            (balance_sheet["comp_type"].isin(["tech","fmcg"]))
                            ]
การวิเคราะห์งบการเงินด้วย Python

มาฝึกกันเถอะ!

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

Preparing Video For Download...