Pandas: ตรวจสอบว่ามีแถวจาก dataframe หนึ่งอยู่ในอีกแถวหนึ่งหรือไม่


คุณสามารถใช้ไวยากรณ์ต่อไปนี้เพื่อเพิ่มคอลัมน์ใหม่ให้กับ Pandas DataFrame ที่ระบุว่าแต่ละแถวมีอยู่ใน DataFrame อื่นหรือไม่:

 #merge two DataFrames on specific columns
all_df = pd. merge (df1, df2, on=[' column1 ', ' column2 '], how=' left ', indicator=' exists ')

#drop unwanted columns
all_df = all_df. drop (' column3 ', axis= 1 )

#add column that shows if each row in one DataFrame exists in another
all_df[' exists '] = np. where (all_df. exists == ' both ', True , False )

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

ตัวอย่าง: ตรวจสอบว่ามีแถวใน Pandas DataFrame อันหนึ่งอยู่ในอีกแถวหนึ่งหรือไม่

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

 import pandas as pd

#create first DataFrame
df1 = pd. DataFrame ({' team ': ['A', 'B', 'C', 'D', 'E'], 
                    ' points ': [12, 15, 22, 29, 24]}) 

print (df1)

  team points
0 to 12
1 B 15
2 C 22
3 D 29
4 E 24

#create second DataFrame
df2 = pd. DataFrame ({' team ': ['A', 'D', 'F', 'G', 'H'],
                    ' points ': [12, 29, 15, 19, 10],
                    ' assists ': [4, 7, 7, 10, 12]})

print (df2)

  team points assists
0 to 12 4
1 D 29 7
2 F 15 7
3 G 19 10
4:10:12

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

 import numpy as np

#merge two dataFrames and add indicator column
all_df = pd. merge (df1, df2, on=[' team ', ' points '], how=' left ', indicator=' exists ')

#drop assists columns
all_df = all_df. drop (' assists ', axis= 1 )

#add column to show if each row in first DataFrame exists in second
all_df[' exists '] = np. where (all_df. exists == ' both ', True , False )

#view updated DataFrame
print (all_df)

  team points exists
0 A 12 True
1 B 15 False
2 C 22 False
3 D 29 True
4 E 24 False

คอลัมน์ใหม่ ที่มีอยู่ บ่งชี้ว่าแต่ละค่าใน ทีม และคอลัมน์ คะแนน ในแต่ละแถวมีอยู่ใน DataFrame ที่สองหรือไม่

จากผลลัพธ์เราจะเห็นได้ว่า:

  • ค่าทีม A และค่าคะแนน 12 มีอยู่ใน DataFrame ที่สอง
  • ไม่มีค่าทีม B และค่าคะแนน 15 ใน DataFrame ที่สอง
  • ไม่มีค่าทีม C และค่าคะแนน 22 ใน DataFrame ที่สอง
  • ค่าทีมเป็น D และค่าคะแนน 29 มีอยู่ใน DataFrame ที่สอง
  • ไม่มีค่าทีม E และค่าคะแนน 24 ใน DataFrame ที่สอง

โปรดทราบว่าคุณสามารถระบุค่าอื่นที่ไม่ใช่ True และ False ในคอลัมน์ ที่มีอยู่ได้ โดยการเปลี่ยนค่าในฟังก์ชัน NumPy Where()

ตัวอย่างเช่น คุณสามารถใช้ “มีอยู่” และ “ไม่มีอยู่” แทนได้ดังนี้:

 #add column to show if each row in first DataFrame exists in second
all_df[' exists '] = np. where (all_df. exists == ' both ', ' exists ', ' not exists ')

#view updated DataFrame
print (all_df)

  team points exists
0 to 12 exists
1 B 15 not exists
2 C 22 not exists
3 D 29 exists
4 E 24 not exists

โปรดทราบว่าค่าในคอลัมน์ ที่มีอยู่ มีการเปลี่ยนแปลง

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

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

Pandas: เพิ่มคอลัมน์จาก DataFrame หนึ่งไปยังอีก DataFrame
นุ่น: รับแถวที่ไม่ได้อยู่ใน DataFrame อื่น
Pandas: วิธีตรวจสอบว่าหลายคอลัมน์เท่ากันหรือไม่

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

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