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
'[코드스테이츠] AI부트캠프2기 > Section 1' 카테고리의 다른 글
[AI부트캠프 2기] - 6~7일차 (feat. 코드스테이츠) (0) | 2021.03.12 |
---|---|
[AI부트캠프 2기] - 5일차 (feat. 코드스테이츠) (0) | 2021.03.11 |
[AI부트캠프 2기] - 4일차 (feat. 코드스테이츠) (0) | 2021.03.09 |
[AI부트캠프 2기] - 3일차 (feat. 코드스테이츠) (0) | 2021.03.08 |
[AI부트캠프 2기] - 1일차 (feat. 코드스테이츠) (0) | 2021.03.04 |
댓글