Python
Missing value2 (결측치2)
KimJ.W
2023. 1. 23. 19:25
최초 작성일 : 2021-12-22
categories: Python
Missing Value : 결측치 확인
data loading
import pandas as pd
covidtotals = pd.read_csv("../input/covid-data/covidtotals.csv")
covidtotals.head()
<실행 화면>
data info
covidtotals.info()
<실행 화면>
data division
● 인구통계 관련 column
● Covid 관련 column
case_vars = ["location", "total_cases", "total_deaths", "total_cases_pm", "total_deaths_pm"]
demo_vars = ["population", "pop_density", "median_age", "gdp_per_capita", "hosp_beds"]
demo_vars column별로 결측치를 측정
covidtotals[demo_vars].isnull().sum(axis = 0) # column별로 결측치를 측정
<실행 화면>
case_vars column별로 결측치를 측정
covidtotals[case_vars].isnull().sum(axis = 0) # column별로 결측치를 측정
<실행 화면>
● case_vars 에는 결측치가 없지만, demo_vars에는 결측치가 있는 것을 확인 할 수 있다.
위의 column들에 각각 수만큼의 결측치를 확인 할 수 있다.
행 방향으로 발생한 결측치 확인
demovars_misscnt = covidtotals[demo_vars].isnull().sum(axis = 1)
demovars_misscnt.value_counts()
<실행 화면>
0 156
1 24
2 12
3 10
4 8
dtype: int64
covidtotals[case_vars].isnull().sum(axis = 1).value_counts()
<실행 화면>
0 210
dtype: int64
인구통계 데이터가 3가지 이상 누락된 국가를 나열하기
["location"] + demo_vars
covidtotals.loc[demovars_misscnt >= 3, ["location"] + demo_vars].T
<실행 화면>
case에는 누락국가가 없지만, 한번 확인
casevars_misscnt = covidtotals[case_vars].isnull().sum(axis = 1)
casevars_misscnt.value_counts()
<실행 화면>
0 210
dtype: int64
covidtotals[covidtotals['location'] == "Hong Kong"]
temp = covidtotals.copy()
temp[case_vars].isnull().sum(axis = 0)
temp.total_cases_pm.fillna(0, inplace = True)
temp.total_deaths_pm.fillna(0, inplace = True)
temp[case_vars].isnull().sum(axis = 0)
<실행 화면>
위의 사진은 결측치가 있는지 없는지를 확인 시켜주는 사진이다.