dropna=False 옵션 사용하여 누락데이터 개수 확인. 옵션을 추가하지 않으면 NaN데이터 제외하고 출력
import seaborn as sns
df = sns.load_dataset('titanic')
print(df.head())
print(df.info())
print(df.value_counts('deck'))
nan_deck = df['deck'].value_counts(dropna=False) # 누락 데이터 개수 확인(dropna=False)
print(nan_deck)
nan_deck = df['deck'].value_counts() # 누락 데이터 개수 제외
print(nan_deck)
'''누락 데이터 찾는 방법
isnull()메소드와 notnull()메소드가 있음
1)isnull() 누락시 True, 유효 시 False
2)notnull() 유효한 데이터 있을 경우 True, 없으면 False
'''
print(df.head().isnull())
survived pclass sex age ... deck embark_town alive alone
0 False False False False ... True False False False
1 False False False False ... False False False False
2 False False False False ... True False False False
3 False False False False ... False False False False
4 False False False False ... True False False False
[5 rows x 15 columns]
print(df.head().notnull())
survived pclass sex age ... deck embark_town alive alone
0 True True True True ... False True True True
1 True True True True ... True True True True
2 True True True True ... False True True True
3 True True True True ... True True True True
4 True True True True ... False True True True
# 누락 데이터 개수 구하기
print(df.head().isnull().sum(axis=0))
survived 0
pclass 0
sex 0
age 0
sibsp 0
parch 0
fare 0
embarked 0
class 0
who 0
adult_male 0
deck 3
embark_town 0
alive 0
alone 0
dtype: int64
# 누락 데이터 제거
missing_data = df.isnull()
for col in missing_data.columns:
missing_count = missing_data[col].value_counts()
try:
print(col, ':', missing_count[True])
except:
print(col, ':', 0)
survived : 0
pclass : 0
sex : 0
age : 177
sibsp : 0
parch : 0
fare : 0
embarked : 2
class : 0
who : 0
adult_male : 0
deck : 688
embark_town : 2
alive : 0
alone : 0
# NaN값이 500개 이상인 열을 모두 삭제
df_thresh = df.dropna(axis=1, thresh=500)
print(df_thresh.columns)
Index(['survived', 'pclass', 'sex', 'age', 'sibsp', 'parch', 'fare',
'embarked', 'class', 'who', 'adult_male', 'embark_town', 'alive',
'alone'],
dtype='object')
deck 열 삭제됨
# age열에 나이 데이터가 없는 행 모두 삭제
df_age = df.dropna(subset=['age'], how='any', axis=0)
print(len(df_age))
print(df_age)
714
survived pclass sex age ... deck embark_town alive alone
0 0 3 male 22.0 ... NaN Southampton no False
1 1 1 female 38.0 ... C Cherbourg yes False
2 1 3 female 26.0 ... NaN Southampton yes True
3 1 1 female 35.0 ... C Southampton yes False
4 0 3 male 35.0 ... NaN Southampton no True
.. ... ... ... ... ... ... ... ... ...
885 0 3 female 39.0 ... NaN Queenstown no False
886 0 2 male 27.0 ... NaN Southampton no True
887 1 1 female 19.0 ... B Southampton yes True
889 1 1 male 26.0 ... C Cherbourg yes True
890 0 3 male 32.0 ... NaN Queenstown no True
[714 rows x 15 columns]
'python' 카테고리의 다른 글
시계열 데이터 (0) | 2022.02.22 |
---|---|
pandas index (0) | 2022.02.22 |
numpy histogram (0) | 2022.02.21 |
누락 데이터 치환 (0) | 2022.02.20 |
pandas matplotlib folium (0) | 2022.02.20 |