카테고리 없음

Pandas 를 활용한 데이터 클린징

keepgroovin' 2019. 11. 25. 17:00

(1)  문자/숫자 걸러내기             

   예제1)

           # 숫자만 남기고 문자 지우기 
           # ['8GB' '16GB' '4GB' '2GB' '12GB' '6GB' '32GB' '24GB' '64GB'] 

           laptops["ram"] = laptops["ram"].str.replace("GB","") 

           unique_ram = laptops["ram"].unique() 
           # ['8', '16', '4', '2', '12', '6', '32', '24', '64'] 

   예제2) str.replace를 두 개를 붙여써도 됩니다 

      1.18kg, 1.56kgs ==> 1.18, 1.56
       
       laptops["weight"] = laptops["weight"].str.replace("kgs","").str.replace("kg","").astype(float)

(2) function을 활용한 방식 

예제)  def clean_c(col):

    col = col.strip()

    col = col.replace("(","")

    col = col.replace(")","")

    return col 


(3) dictionary를 활용한 series.map() 방식 

 

(4) Remove any rows/columns that have missing values.

  ex) no_null_rows = dataframe["column"].dropna(axis = 0)

  ex) no_null_cols = dataframe["column"].dropna(axis = 1)

(5) Fill the missing values with some other values

 

(6) Leave the missing values as is.