본문 바로가기
[코드스테이츠] AI부트캠프2기/Section 1

[AI부트캠프 2기] - 2일차 (feat. 코드스테이츠)

by xper100 2021. 3. 7.

Feature Engineering (2021/3/5)

몰랐던 부분 정리


apply()를 통한 행변환

# toint를 통한 데이터 형태변환

def toint(string):
	return int(string.replace(',', ''))
    
df['column'] = df['column'].apply(toint)

 

※참고할만 한 점※

lambda를 활용한 방식

df.apply(lambda x: int(x.replace('.','')))

 

 

Reference: pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.html


정규식(Regular Expression) - Re.sub을 통한 string manpulation

# "12,576"을 replace() 사용 없이 integer형태로 변환하라

import re
df['매출액'][df['분기'] == "19/6"] = int(re.sub("\D", "","12,578"))
df['매출액'] = df['매출액'].astype(int)

 

 

해석: "\D"를 통해서 comman(',')를 추출하여 빈공간("")으로 대체한 후 나머지를 int()로 변환하는 것.

 

그렇다면 comma만 추출하고자 한다면?

int(re.sub("\d", "","12,578"))

정규식을 통해서 텍스트 마이닝이나 string을 잘 다룰 수 있도록 연습이 극히 필요할 듯 하다.

 

 

Reference: docs.python.org/3/howto/regex.html

 

 

댓글


출처: https://privatedevelopnote.tistory.com/81 [개인노트]