Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

데이터분석 공부하기

[PANDAS] DataFrame 필터링/셀렉션 본문

Python

[PANDAS] DataFrame 필터링/셀렉션

Eileen's 2021. 10. 19. 15:54

1. ~ & isin 활용 :

m = m[~m['번호'].isin(target)]

 

2. .loc 활용 (Row별 selection):

oil_18 = oil_18.loc[oil_18['휘발유'] != 0, :]
unique_count = oil_18.groupby('번호')[['지역','상표','셀프여부']].nunique()
target = unique_count.loc[(unique_count != 1).sum(axis = 1) != 0] 
#행별로 한 개라도 1이 아니면 True반환, 모두 1이라면 False
oil_18.loc[oil_18['번호'].isin(target.index)]

-특정 column 내 특정 조건을 추출하고 싶은 경우

 

3. [] = []

 

4. drop

 

5. List Comprehension 활용:

m = m.loc[[x not in out_all for x in m['번호']]]

 

6. 차집합(difference) 활용:

out = set(df_copy['번호']).difference(set(df['번호'])) 
#이상치 : 필터링전에는 들어가있던 주유소 번호-후에는 없어진 주유소 번호를 차집함으로 구함
diff_dict = dict()
for year in range(2018, 2021): #2020년도 까지 본다.
  opened = len(id_dict[year+1].difference(id_dict[year])) #차집합
  closed = len(id_dict[year].difference(id_dict[year+1]))
  diff_dict[f'{year}_{year+1}'] = [opened, closed]
diff_df = pd.DataFrame(diff_dict, index = ['OPENED','CLOSED'])