나는야 데이터사이언티스트/PYTHON
[Python]문자열 양 끝 공백 또는 문자 제거 - strip(),lstrip(),rstrip()
우주먼지의하루
2020. 5. 11. 03:19
728x90
문자열 양 끝에 있는 공백을 없애야 하는 경우나 특정 문자를 제거하고 싶을 때 사용
-관련 문서
https://docs.python.org/2/library/stdtypes.html?highlight=str.strip#str.strip
5. Built-in Types — Python 2.7.18 documentation
The following sections describe the standard types that are built into the interpreter. Note Historically (until release 2.2), Python’s built-in types have differed from user-defined types because it was not possible to use the built-in types as the basis
docs.python.org
공백 제거
a = " abcd " 일때,
1. 양 끝 공백 제거
a.strip()
2. 왼쪽 공백 제거
a.lstrip()
3. 오른쪽 공백 제거
a.rstrip()
문자 제거
a = "*abcd*" 일 때,
a.strip("*")
python dataframe에서 사용하려면 str.strip()으로 사용하면 됩니다.
#df의 컬럼 a에 대해 적용 시킬 때
df['a'].str.strip()
#df 전체에 다 적용시킬 때
df.apply(lambda x: x.str.strip(), axis = 1)
반응형