도자기 빚는 손 클렌징 Data Cleansing, Missing data 다루기

2019. 11. 11. 19:27카테고리 없음

 

1. 손 클렌징, 도자기를 빚어요 

내 경험상, 이런 매뉴얼 클린징은 지양하는 게 좋다. 새로운 데이터 들어오면 불안정해짐. 

하드 코딩 없는 깔끔한 데이터 관리가 중요함

 

(1) str.replace() : 이상한 character 골라내서 삭제하기 

(2) str.split() : 중간에 섞인 구분자를 두고 데이터 쪼개기 

 

 

2. Working with Missing Data

(1) Pandas의 DataFrame.isnull() method

(2) Pandas의  DataFrame.sum() method 
:  to count the number of null values in each column

ex ) mvc 라는 데이터프레임이 있음

null_counts = mvc.isnull().sum()

null_counts_pct = null_counts / mvc.shape[0] * 100

null_df = pd.DataFrame({'null_counts': null_counts, 'null_pct': null_counts_pct})
# Rotate the dataframe so that rows become columns and vice-versa

null_df = null_df.T.astype(int)

print(null_df)

>> unique_key date time borough location
null_counts 0 0 0 20646 3885  
null_pct 0 0 0 35 6 

 

 

3. imputation 

: for filling in a missing value with a replacement value

 

ex 1) killed_cols = [col for col in mvc.columns if 'killed' in col]

>>  list (<class 'list'>)
['pedestrians_killed', 'cyclist_killed', 'motorist_killed', 'total_killed']


killed_manual_sum = killed.iloc[:,0:3].sum(axis=1)
>>
0        0
1        0
2        0
3        0
4        0
        ..
57859    0
57860    0
57861    0
57862    0
57863    0
Length: 57864, dtype: int64


killed_mask = killed_manual_sum != killed['total_killed' ]

>>0        False 

1        False
2        False
3        False
4        False
         ...  
57859    False
57860    False
57861    False
57862    False
57863    False
Length: 57864, dtype: bool


killed_non_eq = killed[killed_mask]
>> pedestrians_killed cyclist_killed motorist_killed total_killed
3508 0 0 0 NaN
20163 0 0 0 NaN
22046 0 0 1 0.0
48719 0 0 0 NaN
55148 0 0 0 NaN
55699 0 0 0 NaN