การวิเคราะห์ข้อมูลด้วย PandasAI

Kritchai Tadbhusinwat
G-Able
Published in
4 min readApr 24, 2024
Image by Author (generated by AI)

ในปัจจุบันไม่ว่าจะมองไปทางไหนก็จะเจอแต่คนพูดถึง AI (Artificial Intelligence หรือ ปัญญาประดิษฐ์) ว่าสามารถเข้ามาช่วยงานต่าง ๆ ได้ เช่น การ chat ถามตอบ , สรุปบทความ , สร้างภาพ , สร้าง VDO , เขียนโปรแกรม, วิเคราะห์ข้อมูล และต่าง ๆ อีกมากมาย ซึ่ง AI ที่มีความสามารถในกลุ่มนี้จะถูกเรียกว่า Genarative AI ตัวอย่างเช่น ChatGPT , Gemini , Copilot , Claude , Perplexity เป็นต้น

สำหรับในงานด้านการวิเคราะห์ข้อมูล(Data Analytics) โดยทั่วไปก็อาจจะใช้ MS Excel , ใช้การเขียน code ภาษาต่าง ๆ (Python , R , SQL , …) , ใช้ Business intelligence Tool (PowerBI , Tableau , Looker , …) เพื่อทำการหาวิเคราะห์หาข้อมูลเชิงลึก(Insight) ซึ่งไม่ว่าจะเป็นวิธีใดก็ตาม ล้วนต้องอาศัยความรู้ทางด้านเทคนิคและวิธีการใช้งานเครื่องมือนั้น ๆ

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

ตัวอย่างตามใน Video

Video from https://pandas-ai.com/

ในบทความนี้จะกล่าวถึงการเขียน code ภาษา Python โดยใช้ PandasAI ซึ่งเป็น Python library ที่ถูกพัฒนาต่อยอดมาจาก Pandas ที่ได้รับความนิยมในงานด้าน Data Analytics

ข้อมูลที่ใช้เป็นตัวอย่าง

ตารางแสดงรายชื่อทีมที่ได้แชมป์ Premier League

ตัวอย่าง code การใช้ PandasAI

import pandas as pd
from pandasai import SmartDataframe
import os

os.environ["PANDASAI_API_KEY"] = "<API key>"

df_champ = pd.read_csv('Premier_League_Winners.csv',encoding='utf8')
sdf_champ = SmartDataframe(df_champ)

prompt = 'Which team has won the most championships?'

response = sdf_champ.chat(prompt)
print(response)

ผลลัพธ์

The team that has won the most championships is Manchester United.

เรียกดูว่า PandasAI ทำการเขียน code เพื่อหาผลลัพธ์ให้ได้อย่างไร

print(sdf_champ.last_code_generated)
most_championships = {}
for df in dfs:
champions = df['Champions'].value_counts().idxmax()
most_championships[champions] = most_championships.get(champions, 0) + 1
team_with_most_championships = max(most_championships, key=most_championships.get)
result = {'type': 'string', 'value': f'The team that has won the most championships is {team_with_most_championships}.'}

ตัวอย่าง ค่า prompt และ ผลลัพธ์ที่ได้

ทีมไหนได้แชมป์มากที่สุด
The team with the most championships is Manchester United.
Arrange the top 5 teams that have won the most championships and how many times they have won them.
                   Champions
Manchester United 13
Manchester City 7
Chelsea 5
Arsenal 3
Blackburn Rovers 1
Plot a bar chart of top 5 team with number of won

ทั้งนี้หากข้อมูลที่ต้องการวิเคราะห์มีมากว่า 1 ตาราง PandasAI ก็มีความสามารถในการวิเคราะห์ข้อมูลจากหลายแหล่งได้ ดังนี้

ในที่นี้จะเพิ่มข้อมูลตัวอย่างอีก 1 ตาราง

ตารางแสดงชื่อผู้จัดการทีมและสัญชาติ

ตัวอย่าง code การใช้ PandasAI จากข้อมูลหลายตาราง

import pandas as pd
from pandasai import SmartDatalake
import os

os.environ["PANDASAI_API_KEY"] = "<API key>"

df_champ = pd.read_csv('Premier_League_Winners.csv',encoding='utf8')
df_mgr = pd.read_csv('Manager_Nationality.csv',encoding='utf8')

lake = SmartDatalake([df_champ, df_mgr])

prompt = 'Display information of championship by season , team , manager name , nationlity of manager'

response = lake.chat(prompt)
print(response)

ผลลัพธ์

       Season          Champions             Manager Nationality
0 1992-1993 Manchester United Alex Ferguson Scotland
1 1993-1994 Manchester United Alex Ferguson Scotland
2 1994-1995 Blackburn Rovers Kenny Dalglish Scotland
3 1995-1996 Manchester United Alex Ferguson Scotland
4 1996-1997 Manchester United Alex Ferguson Scotland
5 1997-1998 Arsenal Arsène Wenger France
6 1998-1999 Manchester United Alex Ferguson Scotland
7 1999-2000 Manchester United Alex Ferguson Scotland
8 2000-2001 Manchester United Alex Ferguson Scotland
9 2001-2002 Arsenal Arsène Wenger France
10 2002-2003 Manchester United Alex Ferguson Scotland
11 2003-2004 Arsenal Arsène Wenger France
12 2004-2005 Chelsea José Mourinho Portugal
13 2005-2006 Chelsea José Mourinho Portugal
14 2006-2007 Manchester United Alex Ferguson Scotland
15 2007-2008 Manchester United Alex Ferguson Scotland
16 2008-2009 Manchester United Alex Ferguson Scotland
17 2009-2010 Chelsea Carlo Ancelotti Italy
18 2010-2011 Manchester United Alex Ferguson Scotland
19 2011-2012 Manchester City Roberto Mancini Italy
20 2012-2013 Manchester United Alex Ferguson Scotland
21 2013-2014 Manchester City Manuel Pellegrini Chili
22 2014-2015 Chelsea José Mourinho Portugal
23 2015-2016 Leicester City Claudio Ranieri Italy
24 2016-2017 Chelsea Antonio Conte Italy
25 2017-2018 Manchester City Pep Guardiola Spain
26 2018-2019 Manchester City Pep Guardiola Spain
27 2019-2020 Liverpool Jürgen Klopp Germany
28 2020-2021 Manchester City Pep Guardiola Spain
29 2021-2022 Manchester City Pep Guardiola Spain
30 2022-2023 Manchester City Pep Guardiola Spain

เรียกดูว่า PandasAI ทำการเขียน code เพื่อหาผลลัพธ์ให้ได้อย่างไร

print(lake.last_code_executed)
merged_df = pd.merge(dfs[0], dfs[1], on='Manager', how='left')
result = {'type': 'dataframe', 'value': merged_df}

ตัวอย่าง ค่า prompt และผลลัพธ์ที่ได้

Which top 3 nationalities of winning managers?
The top 3 nationalities of winning managers are: Scotland, Spain, Italy
How many scotland won championship?
14
Plot a bar chart of top 3 national with most number of win

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

ทั้งนี้การใช้งาน AI ยังมีประเด็นที่ต้องกังวลอยู่อีกหลายข้อ เช่น

  • ผลลัพธ์ที่ได้จาก AI ในบางครั้งก็ไม่ได้ถูกต้องนัก : อาการหลอนของ AI (AI Hallucination) ให้ผลลัพธ์ที่ไม่สมเหตุผล เช่น การคิดเลขผิด , จริยธรรมปัญญาประดิษฐ์ (AI Ethics) เช่น การเหยียดสีผิว เพศ เชื้อชาติ เป็นต้น ทั้งนี้ AI เป็นเพียงเครื่องมือในการอำนวยความสะดวกให้กับผู้ใช้ ผู้ใช้ AI จึงควรพิจารณาตรวจสอบผลลัพธ์ด้วยว่าถูกต้อง เชื่อถือได้หรือไม่
  • เรื่องการละเมิดลิขสิทธิ์ (Copyright) : โดยเฉพาะ AI ที่ใช้ในการสร้างภาพ เช่น Stable Diffusion, Midjourney, DALL·E เป็นต้น โดย AI นำภาพที่มีลิขสิทธิ์ไปเรียนรู้ จนสามารถสร้างภาพขึ้นมาใหม่ได้ หรือ AI เขียนบทความโดยอิงไปกับบทความที่มีลิขสิทธิ์ ทั้งนี้เรื่องการละเมิดลิขสิทธิ์ก็ขึ้นอยู่กับว่ากฏหมายในประเทศนั้น ๆ จะกำหนดไว้อย่างไร
  • เรื่อง Data Privacy : ข้อมูลที่เป็นความลับขององค์กร , ข้อมูลที่มีความอ่อนไหว , ข้อมูลที่เกี่ยวข้องกับ PDPA ไม่ควรส่งให้ AI ไปตรง ๆ ควรทำการซ่อนหรือแก้ไปเป็นค่าอื่นก่อน , บางองค์กรมีนโยบายห้ามไม่ให้พนักงานใช้ AI ในการทำงานเพื่อหลีกเลี่ยงประเด็นนี้ ดังนั้นองค์กรจึงควรต้องให้ความรู้กับพนักงานในการใช้งานเทคโนโลยี AI ที่ถูกต้องควบคู่ไปด้วย
  • ค่าใช้จ่ายในการใช้งาน : การประมวลผลของ Generative AI ในแต่ละครั้ง ทางบริษัทผู้ผลิต ต้องใช้ทรัพยากรในการประมวลผลจำนวนมากเพื่อให้ได้มาซึ่งผลลัพธ์ ดังนั้นหากนำ AI มาใช้ในสเกลใหญ่จะทำให้มีต้นทุนค่าใช้จ่ายที่มากขึ้น

จากที่ได้กล่าวมา Genarative AI เป็นเทคโนโลยีที่มีประะโยชน์ แต่ก็ยังมีความไม่สมบูรณ์ ในปัจจุบัน AI จึงเหมาะแก่การเอาไว้เป็นตัวช่วย เช่น ช่วยให้โปรแกรมเมอร์ ไม่ต้องมาเขียนเองทั้งหมด สามารถนำ code ที่ได้จาก AI มาปรับแก้ หรือเอามาเป็น idea , ช่วยในการสร้าง presentation ให้สวยงาม , ช่วยในการเกลาภาษาใน email ให้เป็นทางการ เป็นต้น

เพิ่มเติม
-API key สามารถลงทะเบียนรับได้ที่ https://pandabi.ai
-เอกสารการใช้งานสามารถดูเพิ่มเติมได้ที่ https://docs.pandas-ai.com/en/latest/

--

--

Kritchai Tadbhusinwat
G-Able
Writer for

Programmer บริษัทเอกชนที่เป็นส่วนหนึ่งของเบื้องหลังให้กับบริษัทชั้นนำต่าง ๆ และ Tutor สอนกวดวิชาคณิตศาสตร์/ฟิสิกส์ เพื่อเตรียมสอบเข้ามหาวิทยาลัย