Pandas 데이터 조회, 정렬, 조건필터 - loc, iloc, where, isin
Pandas 데이터 조회, 정렬, 조건필터 - loc, iloc, where, isin
Pandas 데이터 조회, 정렬, 조건필터 - loc, iloc, where, isin
데이터 분석
데이터 조회
- 함수값
df.head(): 앞 부분 5개 데이터 확인df.tail(): 뒷 부분 5개 데이터 확인df.info(): 컬럼별 정보 확인 (데이터 개수, 데이터 타입)df.describe(): 컬럼별 요약 통계 확인df.describe(include='object'): 글자인 경우
- 속성값
df.value_counts: 컬럼별 값의 분포 확인df.ndim: 데이터 차원을 확인df.shapedf.indexdf.columnsdf.values
df['pclass'].astype('int32'): 데이터타입 변경
데이터 정렬
- index 기준 정렬
df.sort_index(): 오름차순 정렬df.sort_index(ascending=False): 내림차순 정렬
- value에 대한 정렬
df.sort_values(by='age').head(): 오름차순 정렬df.sort_values(by='age', ascending=False).head(): 내림차순 정렬df.sort_values(by=['fare', 'age']).head(): 2개 이상 컬럼을 기준으로 정렬df.sort_values(by=['fare', 'age'], ascending=[False, True]).head(): 각 컬럼 지정 정렬
loc - indexing / slicing / 조건필터 / 다중조건
- indexing
- loc[행인덱스,열인덱스]
df.loc[5, 'class']df.loc[2:5, ['age', 'fare', 'who']]
- slicing
df.loc[2:5], ['age', 'fare', 'who']]df.loc[2:5, 'class':'deck'].head()
- 조건필터
condition = df['who'] == 'man': 조건 condition 대입df[condition].head(): Case1df.loc[condition].head(): Case2 -> 이걸 추천df.loc[condition, 'age'] = 10
- 다중조건
& , 연산자 사용 condition1 = (df['fare'] > 30)조건1condition2 = (df['who'] == 'woman')조건2df.loc[condition1 & condition2]
iloc
- loc와 유사하지만 index만 허용 / indexing, slicing 모두 사용 가능
df.iloc[[0, 3, 4], [0, 1, 5, 6]]
where
pandas의 where과 numpy의 where은 다름 ```python DataFrame.where(cond, other=nan, inplace=False, axis=None, level=None, errors=’raise’, try_cast=False)
- cond: True/False로 판단될 수 있는 식
- other: condition을 만족하지 못하는 요소에 할당 할 값 ```
df['fare'].where(df['fare'] < 20, 0).tail(10)
isin
- 특정 값의 포함 여부는 isin 함수를 통해 비교
sample['name'].isin(['kim', 'lee'])- 조건 필터링
condition = sample['name'].isin(['kim', 'lee'])sample.loc[condition]
This post is licensed under CC BY 4.0 by the author.