판다스에서 문자열을 다루기 위한 함수 사용법을 배워 보자
pandas.pydata.org/pandas-docs/stable/reference/series.html#api-series-str
Series — pandas 1.2.1 documentation
pandas provides dtype-specific methods under various accessors. These are separate namespaces within Series that only apply to specific data types. Flags Flags refer to attributes of the pandas object. Properties of the dataset (like the date is was record
pandas.pydata.org
예제 데이터
www.data.go.kr/dataset/15012005/fileData.do
공공데이터 포털
국가에서 보유하고 있는 다양한 데이터를『공공데이터의 제공 및 이용 활성화에 관한 법률(제11956호)』에 따라 개방하여 국민들이 보다 쉽고 용이하게 공유•활용할 수 있도록 공공데이터(Datase
www.data.go.kr
CSV 소상공인시장진흥공단_상가(상권)정보
df = pd.read_csv("./상가업소정보_201912_01.csv", sep="|", encoding='cp949')
df.head()
데이터프레임['컬럼명'].str[]
앞에서 5자리까지 문자열 추출
df["도로명주소"].str[:5]
거꾸로 출력
df["도로명주소"].str[5::-1]
시작글자로 추출 .str.startswith("추출문자")
해당 문자로 시작되는 글자가 있다면 True, 아니면 False를 반환하는 boolean indexing을 지원
df["도로명주소"].str.startswith("서울").head()
boolean indexing은 데이터프레임으로 감싸면 True에 해당하는 index를 출력
df[df["도로명주소"].str.startswith("서울")].head()
끝글자로 추출 .str.endswith("추출문자")
df[df["상권업종중분류명"].str.endswith("카페")].head()
문자열 분할 .str.split()
# 공백 기준으로 나누기
df["도로명주소"].str.split(" ")
# expand = True 옵션으로 리스트를 프레임으로 변경가능
df["도로명주소"].str.split(" ", expand=True)
# 리스트 특성을 사용하여 시, 동, 도로명으로 분리 가능
df["도로명주소"].str.split(" ", expand=True)[0]
원하는 문자열 추출
df["시"] = df["도로명주소"].str.split(" ", expand=True)[0]
df["구"] = df["도로명주소"].str.split(" ", expand=True)[1]
df.head()
'Python > 데이터전처리' 카테고리의 다른 글
2021년 유투브 워드클라우드 (0) | 2021.02.21 |
---|---|
Colab 파일 불러 오기 (0) | 2021.01.10 |
pyplot , seaborn 판다스 한글폰트 마이너스 폰트 설정 (0) | 2020.12.30 |