Machine Learning/[Kaggle Course] Data Visualization

[Kaggle Course] Data Types, Missing Values, Replace - Dtype

WakaraNai 2020. 11. 26. 17:04
728x90
반응형

.dtype

- dataframe, series  속 값들의 data type을 알려주는 함수

하나의 column 속 모든 값들의 data type
price column은 실수형만 들어있음.

index는 'int64' type

전체 dataframe의 모든 column에 대해서도 가능
'object type' : string으로 된 column은 특정 type으로 분류x

+) Pandas는 categorical data, timeseries data에 대해서도 분류할 줄 알지만, 그건 다음 튜토리얼에서.

 

 

.astype('~~')

- 해당 column의 전체 값들에 대해서 원하는 data type으로 바꾸기

price column의 'int64' type을 'float64'로 바꿔버림

 

 

 

Missing data (NaN)

- NaN(not a number)는 항상 'float64' dtype로 분류하여, pandas가 인식할 수 있게 함.

그러므로 NaN entires를 찾고 싶다면, 

df[pd.isnull(df.col)] 또는 pd.notnull()을 이용

 

 

해당 NaN 자리를 다른 값으로 채우고 싶다면,

.fillna('다른 값')를 사용

 

 

Replace( ) for values similar to missing values

NaN이 아닌, "Unknown", "Undisclosed","Invalid"와 같은 missing value와 비슷한 값들을 다뤄야할 때 유용.

 

 

 


Exercise

 

 

 

Q1. point column의 NaN 개수 찾기

# way1
n_missing_prices = len(reviews[reviews.price.isnull()])

# way2: boolean type으로 바뀌면, True is treated as 1 and False as 0이므로, sum()을 이용
n_missing_prices = reviews.price.isnull().sum()

# way3 = way2+way1
n_missing_prices = pd.isnull(reviews.price).sum()

 

 

Q2. 가장 많이 와인을 생산하는 지역(region)은?

'region_1' 필드에 값이 존재할 때마다 덧셈을 한 결과를 담은 Series를 생성하세요.

이 필드는 missing data이기도 하므로, missing data를 Unknown으로 replace하시고.

마지막에 내림차순으로 정렬해주세요.

728x90
반응형