การกรองข้อมูลด้วยฟังก์ชัน filter()

การเขียนโค้ดที่มีประสิทธิภาพด้วย pandas

Leonidas Souliotis

PhD Candidate

จุดประสงค์ของ filter()

จำกัดผลลัพธ์ตามคุณลักษณะของการรวมกลุ่ม

  • จำนวนค่าที่หายไป
  • ค่าเฉลี่ยของคุณลักษณะที่ต้องการ
  • จำนวนการปรากฏของกลุ่ม
การเขียนโค้ดที่มีประสิทธิภาพด้วย pandas

กรองข้อมูลด้วย groupby().filter()

restaurant_grouped = restaurant.groupby('day')
filter_trans = lambda x : x['total_bill'].mean() > 20
restaurant_filtered = restaurant_grouped.filter(filter_trans)
Time using .filter() 0.00414085388184 sec
print(restaurant_filtered['tip'].mean())
3.11527607362
print(restaurant['tip'].mean())
2.9982786885245902
การเขียนโค้ดที่มีประสิทธิภาพด้วย pandas

เปรียบเทียบกับวิธีดั้งเดิมของ Python

t=[restaurant.loc[df['day'] == i]['tip'] for i in restaurant['day'].unique() 
    if restaurant.loc[df['day'] == i]['total_bill'].mean()>20]
restaurant_filtered = t[0]
for j in t[1:]: 
    restaurant_filtered=restaurant_filtered.append(j,ignore_index=True)
Time using native Python: 0.00663900375366 sec
print(restaurant_filtered.mean())
3.11527607362
Difference in time: 60.329341317157024%
การเขียนโค้ดที่มีประสิทธิภาพด้วย pandas

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

การเขียนโค้ดที่มีประสิทธิภาพด้วย pandas

Preparing Video For Download...