ThepExcel Logo
  • บทความ
    • Excel
      • Excel ทั่วไป
      • Excel Pivot Table
      • Excel Power Pivot
      • Power Query
      • Excel Array Formula
      • Excel VBA
      • Excel for Business
      • Excel and Maths
      • ฟังก์ชัน Excel ทั้งหมด
    • Power BI
      • Power Query
      • Data Model
      • DAX Formula
      • Power BI Report
    • Coding
      • Excel VBA
      • Python
      • Power Query M Code
    • AI
      • ChatGPT
      • Stable Diffusion
      • MidJourney
    • Highlights : บทความแนะนำ
    • คลิปวีดีโอ
  • อบรม
    • อบรมลูกค้าองค์กร
    • คอร์สออนไลน์ SkillLane
    • แนะนำวิทยากร
    • Excel/Power BI Skill Map
    • Quiz
  • Shop
    • คอร์สออนไลน์
    • สินค้าทั้งหมด
    • หนังสือเล่ม
    • E-Book
    • Cart
  • Download
    • Download ไฟล์จากเทพเอ็กเซล
    • ThepExcel-Mfx : M Code สำเร็จรูป
    • Date Table สำเร็จรูป
    • กราฟ My Skill
    • github.com/ThepExcel
  • รวม Link
    • รวม Link สอน Excel & Power BI ทั้งไทยและเทศ
    • รวม Link เกี่ยวกับ AI
    • รวม Link Coding
    • หนังสือแนะนำ
    • Facebook ThepExcel
    • YouTube ThepExcel
    • DAX Formatter
  • Contact
    • แนะนำ เทพเอ็กเซล (Thep Excel)
    • แนะนำวิทยากร : อาจารย์ ศิระ เอกบุตร (ระ)
    • นโยบายการคุ้มครองข้อมูลส่วนบุคคล (Privacy Policy)
  • Facebook
  • YouTube

หัด Python สำหรับคนเป็น Excel : ตอนที่ 5 – การสร้างฟังก์ชันขึ้นมาใช้เอง (Function)

python function

Categories 📂

Python

Tags 🏷️

function, lambda

เวลาเราเขียนโปรแกรม เราสามารถสั่งให้โปรแกรมทำงานกับข้อมูลด้วยคำสั่งต่างๆ อย่างที่เราได้เรียนรู้ไปในตอนที่แล้ว อย่างไรก็ตาม คำสั่งมาตรฐานอาจจะไม่เพียงพอต่อความต้องการของเรา ดังนั้นวิธีการที่จะช่วยได้คือ เราจะสร้างฟังก์ชันที่ทำงานได้ตรงใจเราขึ้นมาเอง

ลองคิดดูว่าใน Excel version เก่า ยังไม่มีฟังก์ชัน UNIQUE การจะตัดให้เหลือข้อมูลที่ไม่ซ้ำกันมันก็จะยุ่งยาก ต้องเขียนสูตรซ้อนกันหลายบรรทัด แต่พอมีคนสร้างฟังก์ชัน UNIQUE ให้ใช้ใน Excel365 หรือ Excel Online ปุ๊ป ชีวิตก็จะง่ายขึ้นทันที อารมณ์ของการมีฟังก์ชันเจ๋งๆ มันก็คล้ายๆ แบบนี้แหละครับ

การที่มีฟังก์ชันที่ทำงานได้ดั่งใจ ช่วยให้เราสามารถทำงานได้ง่ายขึ้นมากๆ ดังนั้นบทความนี้เราจะมาสร้างฟังก์ชันใช้เองกันครับ

สารบัญ

  • Function คืออะไร?
  • วิธีการสร้างฟังก์ชันใน Python
  • ตัวอย่างการเขียนฟังก์ชัน
    • Print ตามจำนวนครั้ง
    • เลียนแบบ LEFT ใน Excel
    • เลียนแบบ TRIM ใน Excel
  • Recursion
  • Lambda Function
  • ตอนต่อไป
  • สารบัญ Series Python

Function คืออะไร?

ฟังก์ชันคือชุดคำสั่งที่จะทำงานเมื่อเราสั่งมัน โดยที่เราสามารถใส่ input เข้าไปใน Function แล้วมันจะทำงานแล้วสามารถคืนค่าผลลัพธ์เป็น Output ออกมาได้ด้วย (หรือจะทำงานเฉยๆ ไม่คืนค่าก็ได้)

แน่นอนว่าคนที่ใช้ Excel มาอย่างพวกเราน่าจะคุ้นเคยกับฟังก์ชันเป็นอย่างดี ซึ่งใน Excel ก็มีฟังก์ชันให้ใช้หลากหลาย เช่น

  • ฟังก์ชัน LEN (text) สามารถนับจำนวนอักขระใน text ได้ว่ามีกี่ตัว
  • ฟังก์ชัน LEFT (text, [num_chars]) เอาไว้ดึงข้อความ text จากด้านซ้าย ตามจำนวนตัวอักษร num_chars ที่เราต้องการ
  • ฟังก์ชัน EDATE (start_date, months) เอาไว้เลื่อนวันจาก start_date ไปตามจำนวนเดือน months ที่ระบุ
  • ฟังก์ชัน NOW() เอาไว้แสดงวันที่และเวลาปัจจุบัน

ฟังก์ชันแต่ละอันต้องการจำนวน input ไม่เท่ากัน เช่น EDATE กับ LEFT ต้องการ 2 ตัว, LEN ต้องการ 1 ตัว, ส่วน NOW ไม่ต้องการเลย นอกจากนั้น input บางตัวก็เป็นค่า optional คือจะระบุหรือไม่ก็ได้ เช่น [num_chars] ใน LEFT ถ้าไม่ระบุแปลว่าเอา 1 ตัวเป็นต้น

ซึ่งใน Python เราก็สามารถสร้างฟังก์ชันที่ต้องการ input เข้าไปประมวลผลได้เหมือนกัน และทำให้ input เป็น optional หรือไม่ก็ได้ด้วย

วิธีการสร้างฟังก์ชันใน Python

เราสามารถสร้างฟังก์ชันได้ด้วยคำสั่งรูปแบบนี้ (ย่อหน้าข้างใน def สำคัญมาก เป็นตัวระบุว่าบรรทัดไหนคือส่วนของฟังก์ชัน)

def ชื่อฟังก์ชัน():
  คำสั่งที่ต้องการให้ทำในฟังก์ชันนั้นๆ

โดยที่สามารถระบุ input ได้ด้วยแบบนี้

def ชื่อฟังก์ชัน(input1,input2):
  คำสั่งที่ต้องการให้ทำในฟังก์ชันนั้นๆ

และสามารถกำหนดค่า default ของ input แต่ละตัวได้ด้วยการสั่งแบบนี้

def ชื่อฟังก์ชัน(input1=def1,input2):
  คำสั่งที่ต้องการให้ทำในฟังก์ชันนั้นๆ

นอกจากนั้นยังสามารถให้มันคืนค่าผลลัพธ์ออกมาได้เช่นกัน ด้วยคำสั่ง return แบบนี้

def ชื่อฟังก์ชัน(input1=def1,input2):
  คำสั่งที่ต้องการให้ทำในฟังก์ชันนั้นๆ
  return ผลลัพธ์

และเราสามารถเรียกใช้ฟังก์ชันนั้นๆ ได้โดยเรียกชื่อฟังก์ชันตามด้วย input ในวงเล็บ ซึ่งเหมือนกับการเรียกใช้สูตร Excel เลย

มาดูตัวอย่างกันดีกว่าครับ

ตัวอย่างการเขียนฟังก์ชัน

Print ตามจำนวนครั้ง

เริ่มจากตัวอย่างพื้นฐาน คือให้ print คำที่ระบุ ตามจำนวนครั้งที่ต้องการ โดยถ้าไม่ระบุจำนวนครั้งจะให้ print ครั้งเดียว ซึ่งผมจะตั้งชื่อฟังก์ชันว่า printX (เท่มะ?) สามารถทำได้ดังนี้

def printX(text,num_time=1):  #สร้างฟังก์ชัน printX
  for i in range(num_time): #วน loop ตามจำนวนครั้งที่ระบุ
    print(text)

printX("เทพมาก",3)  #เรียกใช้ฟังก์ชัน printX ที่สร้างไว้

เลียนแบบ LEFT ใน Excel

ใน Python ไม่มีฟังก์ชัน LEFT เหมือน Excel แต่เราจะลองสร้างมันขึ้นมาใช้งานบ้าง ซึ่งทำได้ดังนี้

def LEFT(text,num_chars=1): #สร้างฟังก์ชัน LEFT
  return text[:num_chars]

print(LEFT("ThepExcel",4)) #เรียกใช้ฟังก์ชัน LEFT ที่สร้างไว้ แบบระบุ 4 ตัว
print(LEFT("ThepExcel")) #เรียกใช้ฟังก์ชัน LEFT ที่สร้างไว้ แบบไม่ระบุจำนวน (default คือ 1)

เลียนแบบ TRIM ใน Excel

TRIM ใน Excel สามารถ TRIM ช่องว่างตรงกลางได้ด้วย แต่ใน Python เอาตรงกลางไม่ออก (เอาออกแต่หน้าหลัง) เราก็สามารถสร้าง TRIM มาใช้เองใน Pythonได้ แต่จะทำให้เจ๋งขึ้นอีกคือระบุตัวอักขระที่จะ Trim ออกได้ด้วย และเอาตรงกลางออกได้ด้วย ดังนี้

def TRIM(text,trim_char=" "): #สร้างฟังก์ชัน TRIM โดยให้ตัวที่จะ Trim เป็นเครื่องหมาย space
  list1=text.split(trim_char) #split แตก item ออกมาตามตัวคั่น
  print(list1)
  list2=[x for x in list1 if x!='']  #คัดเลือกเอา item ที่ไม่ใช่ช่องว่าง
  print(list2)

  #เอาข้อความใน list2 มาต่อกันด้วย trim_char เหมือนเดิมด้วย ตัวคั่น.join(list)
  outputText=trim_char.join(list2)  
  return outputText

print(TRIM("  Thep    Excel   "))
print(TRIM("...Thep.....Excel....","."))

เวลาใช้จริงก็ไม่ต้อง print step ระหว่างทางหรอก (อันนีั้นเราทำเพื่อตรวจสอบผลลัพธ์) ดังนั้นให้ comment ทิ้งไปเลยแบบนี้ก็ได้

ซึ่งแน่นอนว่าเราเอาฟังก์ชันที่สร้างไปใช้ประโยชน์ได้มากมาย เช่น เอาไปวน Loop ใน List เพื่อ clean ข้อมูลที่ต้องการก็ได้

def TRIM(text,trim_char=" "): #สร้างฟังก์ชัน TRIM 
  list1=text.split(trim_char) 
  list2=[x for x in list1 if x!='']  

  outputText=trim_char.join(list2)  
  return outputText

OriginalList=["  Thep    Excel   ","  Sira  Ekabut     "," Eren  Yeager "]
FixedList=[TRIM(x) for x in OriginalList] #เรียกใช้ฟังก์ชันใน List Comprehension ก็ได้
print(FixedList)

Recursion

Python นั้นรองรับการสร้างฟังก์ชันที่เรียกใช้งานตัวเองได้ด้วย

ยกตัวอย่างเช่น การสร้างการคำนวณ Factorial นั่นคือเอาเลขคูณกับตัวที่น้อยกว่าไปเรื่อยๆ จนถึง 1 (โดยที่ Factorial 0 มีค่าเป็น 1)

def FACT(num): #สร้างฟังก์ชัน Factorial
  if num==0:
    return 1   #ถ้า FACT(0) ให้เป็น 1
  else:
    return num*FACT(num-1)  #นอกนั้นให้เอาเลขนั้น*FACT(เลขน้อยลง)

print(FACT(0))  #คือ 1
print(FACT(1))  #คือ 1*1
print(FACT(2))  #คือ 2*1*1
print(FACT(3))  #คือ 3*2*1*1
print(FACT(4))  #คือ 4*3*2*1*1
print(FACT(10))  #คือ 10*9*8*7*6*5*4*3*2*1*1

Lambda Function

นอกจากการสร้างฟังก์ชันขึ้นมาแบบจริงจังด้วย def แล้ว ใน Python ยังยอมให้เราสร้างสิ่งที่เรียกว่า Lambda Function (เป็น Anonymous Function หรือฟังก์ชันที่ไม่จำเป็นต้องมีชื่อ ) ขึ้นมาด้วย ซึ่งมันเป็นเหมือนการสร้างฟังก์ชันย่อมๆ ที่มี expression ได้แค่อันเดียว แต่จะมี input กี่ตัวก็ได้ ในรูปแบบ

lambda input1,input2,input3,... : expression

เช่น

myFunc=lambda a,b,c : (a*b)+c
print(myFunc(2,3,4))

มีค่าเท่ากับการทำแบบนี้เลย

ตอนต่อไป

ในที่สุดเราก็สามารถสร้างฟังก์ชันขึ้นมาใช้เองได้แล้ว แต่จะดีกว่ามั้ยถ้าเราจะใช้ฟังก์ชัน/ความสามารถเจ๋งๆ ที่คนอื่นได้สร้างไว้แล้วเต็ม internet เลย? ในตอนหน้าเราจะมาพูดถึงการใช้ Module/Packages กันครับ! นี่แหละสิ่งที่ทรงพลังที่สุดในความคิดของผม รอติดตามได้เลย เจ๋งแน่นอน

สารบัญ Series Python

  • หัดเขียนโปรแกรม Python สำหรับคนเป็น Excel มาก่อน : ตอนที่ 1
  • หัด Python สำหรับคนเป็น Excel : ตอนที่ 2 – ประเภทข้อมูล (Data Types)
  • หัด Python สำหรับคนเป็น Excel : ตอนที่ 3 – การวน Loop และ เงื่อนไข if
  • หัด Python สำหรับคนเป็น Excel : ตอนที่ 4 – การทำงานกับ String และ List
  • หัด Python สำหรับคนเป็น Excel : ตอนที่ 5 – การสร้างฟังก์ชันขึ้นมาใช้เอง (Function)
  • หัด Python สำหรับคนเป็น Excel : ตอนที่ 6 – การเรียกใช้ Module / Packages เจ๋งๆ ที่มีคนสร้างไว้แล้ว
  • หัด Python สำหรับคนเป็น Excel : ตอนที่ 7 – Web Scraping ด้วย Beautiful Soup
  • แนวทางการใช้ Python ใน Power BI
  • หัด Python สำหรับคนเป็น Excel : ตอนที่ 8 – การสร้างกราฟด้วย Matplotlib
  • รวม Link สอน Python / Programming / AI/ Machine Learning แบบฟรีๆ

แชร์ความรู้ให้เพื่อนๆ ของคุณ
957    
957    

ติดตามเทพเอ็กเซล

  • Facebook
  • YouTube

อบรมกับเทพเอ็กเซล

🔥 คอร์สใหม่ล่าสุด 🔥

การทำ Optimization ด้วย Excel Solver
สำหรับงานวางแผน
คอร์สออนไลน์ เทพเอ็กเซล
คอร์สออนไลน์ จากเทพเอ็กเซล ดูกี่รอบก็ได้
อบรม Excel / Power BI ให้องค์กรของคุณ

บทความล่าสุด

  • แนวทางฝึกฝน Excel ให้เก่งขึ้น
  • รวม Link เว็บ/เพจเกี่ยวกับ AI
  • วิธีกำหนดท่าทางแบบให้ได้ดั่งใจด้วย ControlNet ใน Stable Diffusion [Part4]
  • วิธีสั่ง Prompt และตั้งค่าใน Stable Diffusion ให้รูปสวยโดนใจ [Part3]
  • วิธีเรียกใช้งาน Model เจ๋งๆ ใน Stable Diffusion [ตอนที่2]
  • วิธีใช้งาน AI สร้างรูปสุดเจ๋งและฟรีด้วย Stable Diffusion ฉบับมือใหม่ [ตอนที่1]
  • 10 ไอเดีย เรียนรู้ Excel ผ่าน ChatGPT AI สุดเจ๋ง

บทความแนะนำ

🔥ฟังก์ชันทั้งหมดใน Excel 🔥

  • แกะเคล็ดวิชา Excel Wizard ในการแข่ง Speed Run Excel ระดับโลก
  • เจาะลึก CALCULATE ใน DAX แบบลึกสุดใจ : Part 1
  • Series สอนดึงข้อมูลจากเว็บ ด้วย Power Automate Desktop
  • สรุปการใช้ LAMBDA ฟังก์ชันที่ใช้สร้างฟังก์ชันใน Excel 365 และผองเพื่อน
  • วิธีใช้ Excel คำนวณระยะเวลาการทำงานรวม แถมระบุเวลาพักได้แบบยืดหยุ่น
  • วิธีจัดการข้อมูลแย่ๆ ด้วย Power Query ทั้งข้อมูลปนกัน ข้อมูลอยู่บนหัวตาราง
  • แยกข้อมูลที่อยู่สุดเน่า ด้วย Excel Power Query

Categories

Tags

ai collection concepts copy database Data Model data validation date dax dropdown error excel filter finance find format formula function game graph IF index intro inventory len link logic lookup match m code merge mid overview paste pivot power query right row solver sort speed split substitute table text time tips trim vba vlookup

Archives

  • April 2023 (3)
  • March 2023 (2)
  • February 2023 (2)
  • January 2023 (1)
  • October 2022 (1)
  • September 2022 (3)
  • August 2022 (3)
  • July 2022 (1)
  • June 2022 (3)
  • May 2022 (1)
  • April 2022 (2)
  • February 2022 (1)
  • December 2021 (2)
  • November 2021 (10)
  • September 2021 (2)
  • August 2021 (6)
  • July 2021 (2)
  • June 2021 (2)
  • May 2021 (10)
  • April 2021 (3)
  • March 2021 (3)
  • February 2021 (4)
  • January 2021 (8)
  • December 2020 (5)
  • November 2020 (13)
  • October 2020 (5)
  • September 2020 (11)
  • August 2020 (4)
  • July 2020 (13)
  • June 2020 (17)
  • May 2020 (16)
  • April 2020 (16)
  • March 2020 (10)
  • February 2020 (15)
  • January 2020 (16)
  • December 2019 (4)
  • November 2019 (3)
  • October 2019 (9)
  • September 2019 (1)
  • August 2019 (7)
  • June 2019 (3)
  • May 2019 (9)
  • April 2019 (9)
  • March 2019 (2)
  • February 2018 (1)
  • January 2018 (3)
  • November 2017 (3)
  • August 2017 (1)
  • July 2017 (1)
  • June 2017 (1)
  • May 2017 (6)
  • April 2017 (6)
  • March 2017 (7)
  • February 2017 (1)
  • January 2017 (2)
  • December 2016 (1)
  • October 2016 (2)
  • September 2016 (3)
  • August 2016 (2)
  • July 2016 (2)
  • June 2016 (1)
  • May 2016 (1)
  • April 2016 (1)
  • March 2016 (2)
  • February 2016 (1)
  • January 2016 (2)
  • December 2015 (2)
  • November 2015 (5)
  • October 2015 (3)
  • June 2015 (2)
  • May 2015 (1)
  • April 2015 (26)
  • January 2015 (1)
  • December 2014 (1)
  • November 2014 (2)
  • October 2014 (1)
  • September 2014 (2)
  • August 2014 (1)
  • June 2014 (1)
  • May 2014 (1)
  • April 2014 (3)
  • March 2014 (3)
  • February 2014 (12)
  • January 2014 (7)
  • December 2013 (2)
  • November 2013 (8)
  • October 2013 (2)

เทพเอ็กเซล : Thep Excel

copyright © 2022

  • Facebook
  • YouTube
เว็บไซต์นี้ใช้คุกกี้ (Cookies)
บริษัท เทพเอ็กเซล จำกัด ให้ความสำคัญต่อข้อมูลส่วนบุคคลของท่าน เพื่อการพัฒนาและปรับปรุงเว็บไซต์รวมถึงสินค้าและบริการต่างๆ หากท่านใช้บริการเว็บไซต์นี้ โดยไม่มีการปรับตั้งค่าใดๆ แสดงว่าท่านยินยอมที่จะรับคุกกี้บนเว็บไซต์ และนโยบายสิทธิส่วนบุคคลของเรา
ตั้งค่าคุกกี้ยอมรับทั้งหมดอ่านเพิ่มเติม
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT