재원's 블로그

Missing value2 (결측치2) 본문

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()

<실행 화면>

covid1

data info

covidtotals.info()

<실행 화면>

MissingValue_covid_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별로 결측치를 측정

<실행 화면>

MissingValue_covid_isnullsum

case_vars column별로 결측치를 측정

covidtotals[case_vars].isnull().sum(axis = 0) # column별로 결측치를 측정

<실행 화면>

MissingValue_covid_nullSum

● case_vars 에는 결측치가 없지만, demo_vars에는 결측치가 있는 것을 확인 할 수 있다.

demo_bars - missing value

위의 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

<실행 화면>

MissingValue_covid_Location

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)

<실행 화면>

MissingValue_covid_Del

위의 사진은 결측치가 있는지 없는지를 확인 시켜주는 사진이다.

'Python' 카테고리의 다른 글

crawlig2 (크롤링2)  (0) 2023.01.23
python def, decorator  (0) 2023.01.23
Missing value (결측치)  (0) 2023.01.23
Order & Find outliers(주문과 이상치 찾기)  (0) 2023.01.23
Trees - Decision Tree about(결정 트리에 대해)  (0) 2023.01.23