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 or column: 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 유지

 


ref