Automatically translated.View original post

🧮 Daily Python Problem: Let's come to average! 💖

Today, let's practice a very common problem in the Data world. This is the Daily Python Problem: Calculate Average!

Problem: Let us write a function named calculation _ average (number) that receives a list of numbers and returns the average of all the numbers in that list.

⚠️ Don't forget to handle the Edge Case!

The most important thing is the Edge Case or special case. If Input is an empty list [] and we try to divide by 0, our program will Error immediately! 😱

• Requirements: If the list is empty, return 0 (or None, whichever we define)

✨ Let's see two great ways to write.

1.Basic type (check len first):

• Check if len (number) = = 0. If yes, return 0.

• If you are busy, use the sum () function to take the sum and len () to take the number and divide it.

2. Pythonic (use if not numbers):

• This method is shorter and popular in Python because if not numbers: it means checking if len (numbers) = = 0:!

• If the list is busy, then calculate and return in one line. Very short and beautiful!

Example:

• Input: calculated _ average ([1, 2, 3, 4, 5])

• Output: 3.0

Try to practice. Edge Case management is an important skill that good developers need! Keep going. 🥰

# data # python # ai # programmer # learnonlemon8

2025/11/11 Edited to

... Read moreนอกจาก “หาค่าเฉลี่ยใน List” แล้ว เวลาเจอคำถามแนว ๆ ในงาน Data มักจะต่อยอดไปที่ “หาค่าเฉลี่ยหลายคอลัมน์ในตาราง” เช่นใน pandas DataFrame (หลายคนค้นหากับชุดข้อมูล mtcars บ่อยมาก) เลยขอเสริมวิธีคิด/ตัวอย่างที่ฉันใช้จริงให้ค่ะ เผื่อเอาไปประยุกต์กับโจทย์ apply() ได้เลย 1) หาค่าเฉลี่ยทุกคอลัมน์ใน DataFrame แบบตรงที่สุด ถ้าต้องการค่าเฉลี่ย “รายคอลัมน์” (เหมือนเอา calculate_average ไปใช้กับแต่ละคอลัมน์) ทำได้ง่ายมาก: - df.mean(numeric_only=True) อันนี้จะได้ Series ของค่าเฉลี่ยแต่ละคอลัมน์ และเลี่ยงคอลัมน์ที่ไม่ใช่ตัวเลขให้อัตโนมัติ 2) ใช้ apply() เพื่อฝึกแนวคิด (เหมือนทำฟังก์ชันเอง) ถ้าโจทย์ระบุให้ใช้ apply() เช่น “หาค่าเฉลี่ยของทุกคอลัมน์ใน mtcars ต้องเขียน apply() ยังไง” ฉันจะทำประมาณนี้: - df.apply(lambda col: col.mean(), axis=0) axis=0 คือทำงานทีละคอลัมน์ ผลลัพธ์เหมือน df.mean() 3) จัดการ Edge case แบบเดียวกับ List ว่าง (สำคัญมาก) ใน DataFrame ก็มีเคสคล้าย ๆ “List ว่าง” เช่นคอลัมน์มีแต่ค่าว่าง (NaN) หรือกรองข้อมูลแล้วไม่เหลือแถวเลย - ถ้าเป็น NaN: โดยปกติ mean() จะข้าม NaN ให้ แต่ถ้าทั้งคอลัมน์เป็น NaN ผลจะเป็น NaN - ถ้าอยากให้ fallback เป็น 0 แบบโจทย์ calculate_average ให้ใช้: df.apply(lambda col: 0 if col.dropna().empty else col.mean(), axis=0) แนวคิดเดียวกับ if not numbers: คือเช็ค “ว่างจริงไหม” ก่อนหาร 4) ตัวอย่างการประยุกต์กับคอลัมน์ที่ไม่ใช่ตัวเลข บางทีในตารางมีคอลัมน์ชื่อรุ่น/ประเภทปนมา ถ้าใช้ apply ตรง ๆ อาจ error ได้ ฉันจะเลือกเฉพาะ numeric ก่อน: - num_df = df.select_dtypes(include="number") - num_df.apply(lambda col: col.mean(), axis=0) 5) “Problem list” เขียนยังไงให้ชัด (เผื่อทำโน้ต/โพสต์/การบ้าน) เวลาฉันทำโจทย์แนว Daily Python Problem จะเขียนเป็น problem list สั้น ๆ แบบนี้เพื่อกันหลุดประเด็น: - Input คืออะไร (List ตัวเลข / DataFrame) - Output ต้องเป็นอะไร (ค่าเฉลี่ย float) - Constraints/Edge case (ลิสต์ว่าง return 0, คอลัมน์ว่าง/NaN ทำยังไง) - วิธีแก้ (ใช้ sum() + len(), หรือ mean(), หรือ apply()) - ตัวอย่างทดสอบ (เช่น [1,2,3,4,5] -> 3.0, [10,20] -> 15.0) ถ้าทำ 2 สเต็ปนี้จนคล่อง (เช็คว่าง + คำนวณแบบสั้น) จะต่อยอดจาก List ไป DataFrame ได้ไวมาก และลดบั๊กเรื่องหารศูนย์/ข้อมูลหายได้เยอะจริง ๆ ค่ะ