วิธีแปลงตัวแปรหมวดหมู่เป็นตัวเลขใน pandas


คุณสามารถใช้ไวยากรณ์พื้นฐานต่อไปนี้เพื่อแปลงตัวแปรหมวดหมู่เป็นตัวแปรตัวเลขใน Pandas DataFrame:

 df[' column_name '] = pd. factorize (df[' column_name '])[0]

คุณยังสามารถใช้ไวยากรณ์ต่อไปนี้เพื่อแปลงตัวแปรประเภทแต่ละรายการใน DataFrame เป็นตัวแปรตัวเลข:

 #identify all categorical variables
cat_columns = df. select_dtypes ([' object ']). columns

#convert all categorical variables to numeric
df[cat_columns] = df[cat_columns]. apply ( lambda x: pd.factorize (x)[ 0 ])

ตัวอย่างต่อไปนี้แสดงวิธีใช้ไวยากรณ์นี้ในทางปฏิบัติ

ตัวอย่างที่ 1: แปลงตัวแปรหมวดหมู่เป็นตัวเลข

สมมติว่าเรามี DataFrame แพนด้าดังต่อไปนี้:

 import pandas as pd

#createDataFrame
df = pd. DataFrame ({' team ': ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'],
                   ' position ': ['G', 'G', 'F', 'G', 'F', 'C', 'G', 'F', 'C'],
                   ' points ': [5, 7, 7, 9, 12, 9, 9, 4, 13],
                   ' rebounds ': [11, 8, 10, 6, 6, 5, 9, 12, 10]})

#view DataFrame
df

team position points rebounds
0 A G 5 11
1 A G 7 8
2 A F 7 10
3 B G 9 6
4 B F 12 6
5 B C 9 5
6 C G 9 9
7 C F 4 12
8 C C 13 10

เราสามารถใช้ไวยากรณ์ต่อไปนี้เพื่อแปลงคอลัมน์ “ทีม” เป็นตัวเลข:

 #convert 'team' column to numeric
df[' team '] = pd. factorize (df[' team '])[ 0 ]

#view updated DataFrame
df

team position points rebounds
0 0 G 5 11
1 0 G 7 8
2 0 F 7 10
3 1 G 9 6
4 1 F 12 6
5 1 C 9 5
6 2 G 9 9
7 2 F 4 12
8 2 C 13 10

การเปลี่ยนแปลงเกิดขึ้นดังนี้:

  • แต่ละทีมที่มีค่า ” A ” จะถูกแปลงเป็น 0
  • แต่ละทีมที่มีค่า ” B ” จะถูกแปลงเป็น 1
  • แต่ละทีมที่มีค่า ” C ” จะถูกแปลงเป็น 2

ตัวอย่างที่ 2: แปลงตัวแปรหมวดหมู่หลายรายการให้เป็นค่าตัวเลข

สมมติว่าเรามี DataFrame แพนด้าดังต่อไปนี้:

 import pandas as pd

#createDataFrame
df = pd. DataFrame ({' team ': ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'],
                   ' position ': ['G', 'G', 'F', 'G', 'F', 'C', 'G', 'F', 'C'],
                   ' points ': [5, 7, 7, 9, 12, 9, 9, 4, 13],
                   ' rebounds ': [11, 8, 10, 6, 6, 5, 9, 12, 10]})

#view DataFrame
df

        team position points rebounds
0 A G 5 11
1 A G 7 8
2 A F 7 10
3 B G 9 6
4 B F 12 6
5 B C 9 5
6 C G 9 9
7 C F 4 12
8 C C 13 10

เราสามารถใช้ไวยากรณ์ต่อไปนี้เพื่อแปลงตัวแปรแต่ละหมวดหมู่ใน DataFrame เป็นตัวแปรตัวเลข:

 #get all categorical columns
cat_columns = df. select_dtypes ([' object ']). columns

#convert all categorical columns to numeric
df[cat_columns] = df[cat_columns]. apply ( lambda x: pd.factorize (x)[ 0 ])

#view updated DataFrame
df

	team position points rebounds
0 0 0 5 11
1 0 0 7 8
2 0 1 7 10
3 1 0 9 6
4 1 1 12 6
5 1 2 9 5
6 2 0 9 9
7 2 1 4 12
8 2 2 13 10

โปรดทราบว่าคอลัมน์หมวดหมู่ทั้งสองคอลัมน์ (ทีมและตำแหน่ง) ได้รับการแปลงเป็นตัวเลขแล้ว ในขณะที่คอลัมน์คะแนนและรีบาวด์ยังคงเหมือนเดิม

หมายเหตุ : คุณสามารถดูเอกสารฉบับเต็มของฟังก์ชัน pandas factorize() ได้ที่นี่

แหล่งข้อมูลเพิ่มเติม

บทช่วยสอนต่อไปนี้จะอธิบายวิธีดำเนินการทั่วไปอื่น ๆ ในแพนด้า:

วิธีแปลงคอลัมน์ Pandas DataFrame เป็นสตริง
วิธีแปลงคอลัมน์ Pandas DataFrame เป็นจำนวนเต็ม
วิธีแปลงสตริงให้ลอยใน Pandas DataFrame

เพิ่มความคิดเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องข้อมูลจำเป็นถูกทำเครื่องหมาย *