แพ็กเกจ

Python ระดับกลางสำหรับนักพัฒนา

Jasmin Ludolf

Senior Data Science Content Developer

โมดูลคือไฟล์ Python

  • โมดูล = ไฟล์ Python

  • ใครก็สร้างไฟล์ Python ได้!

ไฟล์โค้ดบนแล็ปท็อป

Python ระดับกลางสำหรับนักพัฒนา

แพ็กเกจ

  • กลุ่มของโมดูล = แพ็กเกจ
    • เรียกอีกชื่อว่า ไลบรารี
  • เผยแพร่สาธารณะและใช้ได้ฟรี
  • ดาวน์โหลดจาก PyPI
  • จากนั้นนำเข้าและใช้งานได้เหมือนโมดูล

กล่องกระดาษแข็งขนาดใหญ่

1 https://pypi.org/
Python ระดับกลางสำหรับนักพัฒนา

การติดตั้งแพ็กเกจ

  • Terminal / Command Prompt

    python3 -m pip install <package_name>
    
  • python3 - รันโค้ด Python จาก terminal

  • pip - ตัวติดตั้งที่แนะนำ

เทอร์มินัลเขียนโค้ด

Python ระดับกลางสำหรับนักพัฒนา

การติดตั้งแพ็กเกจ

 

python3 -m pip install pandas

โลโก้ Pandas

$$

  • แพ็กเกจสำหรับการจัดการและวิเคราะห์ข้อมูล
Python ระดับกลางสำหรับนักพัฒนา

การนำเข้าโดยใช้ alias

# Import pandas
import pandas
  • ใช้ alias เพื่อย่นโค้ดให้สั้นลง
# Import pandas using an alias
import pandas as pd
Python ระดับกลางสำหรับนักพัฒนา

การสร้าง DataFrame

# Sales dictionary
sales = {"user_id": ["KM37", "PR19", "YU88"],
         "order_value": [197.75, 208.21, 134.99]}

# Convert to a pandas DataFrame sales_df = pd.DataFrame(sales)
print(sales_df)
  user_id  order_value
0    KM37       197.75
1    PR19       208.21
2    YU88       134.99
Python ระดับกลางสำหรับนักพัฒนา

การอ่านไฟล์ CSV

# Reading in a CSV file in our current directory
sales_df = pd.read_csv("sales.csv")

# Checking the data type print(type(sales_df))
pandas.core.frame.DataFrame
Python ระดับกลางสำหรับนักพัฒนา

การดูตัวอย่างไฟล์

# DataFrame method to preview the first five rows
print(sales_df.head())
  user_id  order_value
0    KM37       197.75
1    PR19       208.21
2    YU88       134.99
3    NT43       153.54        
4    IW06       379.47
Python ระดับกลางสำหรับนักพัฒนา

การตรวจสอบข้อมูลของไฟล์

# Checking the file info
print(sales_df.info())
RangeIndex: 3 entries, 0 to 2
Data columns (total 2 columns):
 #   Column       Non-Null Count  Dtype  
<hr />  ------       --------------  -----  
 0   user_id      3 non-null      object 
 1   order_value  3 non-null      float64
dtypes: float64(1), object(1)
memory usage: 180.0+ bytes
Python ระดับกลางสำหรับนักพัฒนา

ฟังก์ชันและเมธอด

# This is a built-in function
print(sum([1, 2 ,3, 4, 5]))
15
  • ฟังก์ชัน = โค้ดที่ใช้ทำงานอย่างใดอย่างหนึ่ง
# This is a pandas function
sales_df = pd.DataFrame(sales)
  • .head() ใช้ได้เฉพาะกับ pandas DataFrame
# This is a method
print(sales_df.head())
  user_id  order_value
0    KM37       197.75
1    PR19       208.21
2    YU88       134.99
3    NT43       153.54        
4    IW06       379.47
  • เมธอด = ฟังก์ชันที่ใช้เฉพาะกับชนิดข้อมูลนั้น ๆ
Python ระดับกลางสำหรับนักพัฒนา

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

Python ระดับกลางสำหรับนักพัฒนา

Preparing Video For Download...