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로 효율적인 코드 작성하기

네이티브 방법과 비교

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...