Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- sklearn
- computer vision
- index
- 비트와이즈
- 이미지프로세싱
- ML
- 결측치
- opencv
- scikit-learn
- k-최근접 이웃 분류
- dataframe
- pandas
- 지도학습
- 판다스
- 알파브렌딩
- NULL
- 파이썬
- Deep learning
- 이미지처리
- Python
- 사이킷런
- mask detection
- tfidfvectorizer
- 머신러닝
- 결측값
- NAN
- dropna
- KNeighborsClassifier
- Supervised learning
- 데이터프레임
Archives
- Today
- Total
Sun.El Data Analysis
[Pandas] 데이터프레임 내 결측값 제거 본문
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열 전체가 삭제가 되었다.
'Pandas' 카테고리의 다른 글
[판다스] 특정 조건 행 삭제하기 (0) | 2024.05.27 |
---|---|
[판다스] DataFrame 칼럼 순서 및 이름 변경하기 (0) | 2023.07.15 |
[판다스] 데이터프레임 행/열 추가 삭제 (0) | 2023.07.15 |
[판다스] 데이터프레임 다루기 : df.loc[], df.iloc[] (0) | 2023.07.15 |
[판다스] 결측치/비결측치 확인 : isnull(), notnull() (0) | 2023.07.15 |