python_package/pandas
pandas.dropna()
Xenrose
2024. 10. 11. 23:42
아래 내용은 pandas 2.0.3 버전으로 작성됨.
0. 바로 사용하기
DataFrame.dropna(axis=0,
how='any',
subset=['columns_label'],
inplace=False
)
axis=
0 행을 삭제 / 1 열을 삭제how=
'any' 1개만 Nan이여도 삭제 / 'all' 전부 Nan이면 삭제subset=
입력받은 list 내 칼럼명만 조사inplace=
True면 원본 수정 / False면 원본 수정 안함.(새 변수에 초기화 필요)
1. 기본형
- DataFrame
DataFrame.dropna(*, axis=0, how='any', thresh=0, subset=['columns_label'], inplace=False, # True or True ignore_index=False # True or True)
- Series
Series.dropna(*, axis=0, inplace=False, how=None, # 공식 문서에 정의는 되어있으나 사용하지 않음. ignore_index=False)
- Index
Index.dropna(how='any')
2. 기능
DataFrame/Series/index 객체 내 missing values를 제거하여 반환해줌.
3. 파라미터
axis
axis = 0
or'index'
: Nan값이 있는 행을 삭제axis = 1
orcolumn
: Nan값이 있는 열을 삭제
how
how = 'any'
: Nan값이 1개라도 있을 경우 제거how = 'all'
: 모든 값이 Nan일 경우 제거
thresh
thresh = n
: 제거하려는 행(or 열) 내 Nan값이 n개 미만일 경우 제거how
파라미터와 함께 사용 불가
subset
- 삭제할 column을 지정
df.dropna(axis=0, subset=['delete_column1', 'delete_column2']
inplace
inplace = True
일 경우 원본 객체를 수정함.inplace = False
일 경우 원본 객체를 수정하지 않음.
(추가로 변수에 저장하는 과정이 필요)
ignore_index
ignore_index = True
: Nan값 삭제 후 index를 0부터 새롭게 설정ignore_index = False
: Nan값 삭제 후 index 유지