เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การคำนวณอัตราส่วนทางการเงินด้วยฟังก์ชันที่กำหนดเอง

ในแบบฝึกหัดนี้ จะใช้ฟังก์ชันจากแบบฝึกหัดที่แล้วเพื่อคำนวณอัตราส่วนทางการเงิน โดยฟังก์ชันมีโครงสร้างดังนี้

def compute_ratio(df, numerator, denominator, ratio_name, 
                  addition_in_numerator = True,
                  addition_in_denominator = True):
  numerator_of_ratio = np.where(addition_in_numerator,
                             df[numerator].sum(axis=1), 
                             df[numerator[0]] - df[numerator[1:]].sum(
                               axis=1))
  denominator_of_ratio = np.where(addition_in_denominator, 
                               df[denominator].sum(axis=1), 
                               df[denominator[0]] - df[denominator[1:]].sum(axis=1))
  df[ratio_name] = numerator_of_ratio/denominator_of_ratio
  return df

DataFrame merged_dat ของ pandas ถูกโหลดมาให้แล้ว และจะนำมาใช้คำนวณอัตราส่วนทางการเงินผ่านฟังก์ชัน compute_ratio

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

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

ดูคอร์ส

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Print the columns of merged_dat
print(merged_dat____)
แก้ไขและรันโค้ด