데이터분석 공부하기
[PANDAS] DataFrame 필터링/셀렉션 본문
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'])
'Python' 카테고리의 다른 글
[날짜] 날짜데이터 Transform하는 방법 (0) | 2021.10.19 |
---|---|
[Loop] For문으로 List 반복문 만들기 (0) | 2021.10.19 |
[판다스] 함수(list comprehension, lambda, map, apply, applymap) (0) | 2021.10.19 |
[Reshape] 데이터 재구조화 (0) | 2021.10.19 |
[시각화] Matplotlib & Seaborn (0) | 2021.10.19 |