Python(Colab) 데이터프레임 결측값(isna,isnull,notnull,fillna,dropna)
2023. 6. 8. 15:51ㆍ파이썬
#isna() - 결측값이 있는지 True/False로 반환
df.isna()
#isnull()-결측값이 있는지 True/False로 반환
df.isnull()

1. 조건: group의 결측값이 있는 사람 찾기
df[df['group'].isna()]

2. 조건: group의 결측값이 있는 사람의 인덱스와 이름만 가져오기
df['name'][df['group'].isna()]

3. 조건 : group의 null이 아닌 사람만 찾기
df[df['group'].notnull()]

4 조건: 그룹이 있는 연예인의 name,height,BT을 loc를 사용해서 출력
df.loc[:,['name','height','BT']][df['group'].notnull()]
df.loc[df['group'].notnull(),['name','height','BT']]

결측값 채우기
fillna(): 결측값을 채워주는 함수
조건1 height의 결측값이 있는 사람을 0으로 채우기
df['height'].fillna(0,inplace=True)

조건2. 키의 평균을 구해서 height 결측값의 대입
height = df2['height'].mean()
df2['height'] = df2['height'].fillna(height)
df2['height']
결측값있는 행 제거하기
dropna(): 결측값을 value로 가지고 있는 행을 제거
# dropna() - 결측값이 한개라도 있는 경우 행을 삭제 (인플레이스 X)
df.dropna()
# 결측값이 있는 열을 제거하기 (거의안씀)
df.dropna(axis=1)

728x90
'파이썬' 카테고리의 다른 글
Python(Colab) 데이터프레임 통계 값 다루기 (0) | 2023.06.08 |
---|---|
Python(Colab) 데이터프레임 행렬 추가 or 제거하기 (0) | 2023.06.08 |
Python(Colab) 데이터프레임 인덱싱(loc,iloc,boolean) (0) | 2023.06.08 |
Python Pandas 기초 (0) | 2023.06.08 |
Python Numpy 모듈 (0) | 2023.05.29 |