วิธีตรวจสอบว่ามีคอลัมน์อยู่ใน pandas หรือไม่ (พร้อมตัวอย่าง)


คุณสามารถใช้วิธีการต่อไปนี้เพื่อตรวจสอบว่ามีคอลัมน์อยู่ใน DataFrame ของ pandas หรือไม่:

วิธีที่ 1: ตรวจสอบว่ามีคอลัมน์อยู่หรือไม่

 ' column1 ' in df. columns

สิ่งนี้จะส่งคืน True หากมี “column1” อยู่ใน DataFrame มิฉะนั้นจะส่งคืน ค่า False

วิธีที่ 2: ตรวจสอบว่ามีหลายคอลัมน์หรือไม่

 {' column1 ', ' column2 '}. issubset ( df.columns )

สิ่งนี้จะส่งคืน ค่า True หากมี “column1” และ “column2” อยู่ใน DataFrame มิฉะนั้นจะส่งคืน ค่า False

ตัวอย่างต่อไปนี้แสดงวิธีการใช้แต่ละวิธีในทางปฏิบัติกับ Pandas DataFrame ต่อไปนี้:

 import pandas as pd

#createDataFrame
df = pd. DataFrame ({' team ': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                   ' points ': [18, 22, 19, 14, 14, 11, 20, 28],
                   ' assists ': [5, 7, 7, 9, 12, 9, 9, 4],
                   ' rebounds ': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
print (df)

  team points assists rebounds
0 A 18 5 11
1 B 22 7 8
2 C 19 7 10
3 D 14 9 6
4 E 14 12 6
5 F 11 9 5
6 G 20 9 9
7:28 4 12

ตัวอย่างที่ 1: ตรวจสอบว่ามีคอลัมน์อยู่หรือไม่

เราสามารถใช้โค้ดต่อไปนี้เพื่อดูว่าคอลัมน์ ‘ทีม’ มีอยู่ใน DataFrame หรือไม่:

 #check if 'team' column exists in DataFrame
' team ' in df. columns

True

คอลัมน์ “ทีม” มีอยู่ใน DataFrame ดังนั้นแพนด้าจึงส่งกลับค่า True

นอกจากนี้เรายังสามารถใช้คำสั่ง if เพื่อดำเนินการได้หากมีคอลัมน์ “ทีม” อยู่:

 #if 'team' exists, create new column called 'team_name'
if ' team ' in df. columns :
    df[' team_name '] = df[' team ']
    
#view updated DataFrame
print (df)

  team points assists rebounds team_name
0 A 18 5 11 A
1 B 22 7 8 B
2 C 19 7 10 C
3 D 14 9 6 D
4 E 14 12 6 E
5 F 11 9 5 F
6 G 20 9 9 G
7:28 a.m. 4:12 p.m.

ตัวอย่างที่ 2: ตรวจสอบว่ามีหลายคอลัมน์หรือไม่

เราสามารถใช้รหัสต่อไปนี้เพื่อดูว่าคอลัมน์ ‘ทีม’ และ ‘ผู้เล่น’ มีอยู่ใน DataFrame หรือไม่:

 #check if 'team' and 'player' columns both exist in DataFrame
{' team ', ' player '}. issubset ( df.columns )

False

คอลัมน์ ‘ทีม’ มีอยู่ใน DataFrame แต่ ‘ผู้เล่น’ ไม่มี ดังนั้นแพนด้าจึงส่งกลับค่า เท็จ

นอกจากนี้เรายังสามารถใช้รหัสต่อไปนี้เพื่อดูว่า “คะแนน” และ “ช่วยเหลือ” มีอยู่ใน DataFrame หรือไม่:

 #check if 'points' and 'assists' columns both exist in DataFrame
{' points ', ' assists '}. issubset ( df.columns )

True

มีทั้งสองคอลัมน์ ดังนั้น pandas จึงส่งกลับค่า True

จากนั้นเราสามารถใช้คำสั่ง if เพื่อดำเนินการได้หากมี “คะแนน” และ “ผู้ช่วยเหลือ” อยู่:

 #if both exist, create new column called 'total' that finds sum of points and assists
if {' points ', ' assists '}. issubset ( df.columns ):
    df[' total '] = df[' points '] + df[' assists ']
    
#view updated DataFrame
print (df)

     team points assists rebounds total
0 A 18 5 11 23
1 B 22 7 8 29
2 C 19 7 10 26
3 D 14 9 6 23
4 E 14 12 6 26
5 F 11 9 5 20
6 G 20 9 9 29
7:28 4 12 32

เนื่องจากทั้ง “คะแนน” และ “ช่วยเหลือ” มีอยู่ใน DataFrame แพนด้าจึงสร้างคอลัมน์ใหม่ชื่อ “รวม” ซึ่งแสดงผลรวมของคอลัมน์ “คะแนน” และ “ช่วยเหลือ”

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

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

วิธีรักษาบางคอลัมน์ใน Pandas
วิธีเลือกคอลัมน์ตามดัชนีใน Pandas
วิธีย้ายคอลัมน์ใน Pandas

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

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