Sun.El Data Analysis

[Pandas] 데이터프레임 내 결측값 제거 본문

Pandas

[Pandas] 데이터프레임 내 결측값 제거

Sun.El 2024. 5. 24. 19:33
728x90
데이터 프레임 내에서 결측값을 확인하고 처리하기

 

1. 데이터 확인하기

  • [IN]
import pandas as pd
import seaborn as sns
import numpy as np

df = sns.load_dataset('planets')
df.head(10)

 

  • [OUT]
method	number	orbital_period	mass	distance	year
0	Radial Velocity	1	269.300	7.10	77.40	2006
1	Radial Velocity	1	874.774	2.21	56.95	2008
2	Radial Velocity	1	763.000	2.60	19.84	2011
3	Radial Velocity	1	326.030	19.40	110.62	2007
4	Radial Velocity	1	516.220	10.50	119.47	2009
5	Radial Velocity	1	185.840	4.80	76.39	2008
6	Radial Velocity	1	1773.400	4.64	18.15	2002
7	Radial Velocity	1	798.500	NaN	21.41	1996
8	Radial Velocity	1	993.300	10.30	73.10	2008
9	Radial Velocity	2	452.800	1.99	74.79	2010

 

  • [IN] .info()로 데이터 프레임 정보 확인
df.info()

 

  • [OUT]
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1035 entries, 0 to 1034
Data columns (total 6 columns):
 #   Column          Non-Null Count  Dtype  
---  ------          --------------  -----  
 0   method          1035 non-null   object 
 1   number          1035 non-null   int64  
 2   orbital_period  992 non-null    float64
 3   mass            513 non-null    float64
 4   distance        808 non-null    float64
 5   year            1035 non-null   int64  
dtypes: float64(3), int64(2), object(1)
memory usage: 48.6+ KB

 

위의 변수 정보를 보면 method, number, year은 행의 수와 동일한 1,035개인 반면, 

orbital_period, mass, distance는 그 이하로 결측치가 있는 것을 알 수 있다.

 

2. 결측치가 들어 있는 행 전체 삭제

  • [IN] .dropna()로 결측치를 제거,  axis = 0은 행 방향(기본값), axis = 1은 열 방향인 것을 기억하자!
df2 = df.dropna() #axis = 0 행 삭제 기본값
df2.head(10)

 

dropna()를 사용하여 결측치가 포함된 행 전체를 삭제 할 수 있다. 

이 때는 옵션 axis = 0가 생략되어 있다.

 

  • [OUT]
	method	number	orbital_period	mass	distance	year
0	Radial Velocity	1	269.300	7.10	77.40	2006
1	Radial Velocity	1	874.774	2.21	56.95	2008
2	Radial Velocity	1	763.000	2.60	19.84	2011
3	Radial Velocity	1	326.030	19.40	110.62	2007
4	Radial Velocity	1	516.220	10.50	119.47	2009
5	Radial Velocity	1	185.840	4.80	76.39	2008
6	Radial Velocity	1	1773.40 4.64    18.15	2002
8	Radial Velocity	1	993.300	10.30	73.10	2008
9	Radial Velocity	2	452.800	1.99	74.79	2010
10	Radial Velocity	2	883.000	0.86	74.79	2010

 

결측치가 들어있던 7번행이 삭제되었다. 

 

2-1. 특정 열을 대상으로 결측치가 포함된 행 삭제

  • [IN]
mass = df[['mass']].dropna()
mass.head(10)

 

  • [OUT]
	mass
0	7.10
1	2.21
2	2.60
3	19.40
4	10.50
5	4.80
6	4.64
8	10.30
9	1.99
10	0.86

 

결측치가 들어있던 mass 열의 7번 행이 삭제되었다.

 

3. 결측치가 들어 있는 열 전체 삭제

  • [IN]
df3 = df.dropna(axis = 1) #axis = 0 행 삭제, asis = 1 열 삭제
df3.head(10)

 

  • [OUT]
	method	        number	year
0	Radial Velocity	1	2006
1	Radial Velocity	1	2008
2	Radial Velocity	1	2011
3	Radial Velocity	1	2007
4	Radial Velocity	1	2009
5	Radial Velocity	1	2008
6	Radial Velocity	1	2002
7	Radial Velocity	1	1996
8	Radial Velocity	1	2008
9	Radial Velocity	2	2010

 

결측치가 들어있던 orbital_period, mass, distance 열 전체가 삭제되었다.

 

3-1. 특정 행을 대상으로 결측치가 포함된 열 삭제

  • [IN]
df4 = df[4:10].dropna(axis = 1)
df4.head(10)

 

  • [OUT]
        method	number	orbital_period	distance  year
4	Radial Velocity	1	516.22	119.47	2009
5	Radial Velocity	1	185.84	76.39	2008
6	Radial Velocity	1	1773.40	18.15	2002
7	Radial Velocity	1	798.50	21.41	1996
8	Radial Velocity	1	993.30	73.10	2008
9	Radial Velocity	2	452.80	74.79	2010

 

4~9행 사이에 mass값이 7행에서 결측치가 있었기 때문에, mass열 전체가 삭제가 되었다.