본문 바로가기

코딩/판다승(판다스공부하는희승)

[pandas] 문자열 처리.str(1): 대/소문자 변경, 문자 분류

대/소문자 변경

pandas.Series.str.capitalize():

시리즈나/인덱스에 있는 문자열의 첫글자는 대문자로 그 외에는 소문자로 바꿔준다.

pandas.Series.str.upper():

시리즈나/인덱스에 있는 문자열을 모두 대문자로 바꿔준다.

pandas.Series.str.lower():

시리즈나/인덱스에 있는 문자열을 모두 소문자로 바꿔준다.

pandas.Series.str.title():

시리즈나/인덱스에 있는 문자열의 각 단어의 첫 번째 문자를 대문자로 바꿔주고 그외에는 소문자로 변경한다.

pandas.Series.str.swapcase():

시리즈나/인덱스에 있는 문자열을 대문자인 경우 소문자로, 소문자인 경우 대문자로 바꿔준다.

pandas.Series.str.casefold():

시리즈나/인덱스에 있는 문자열에서 모든 대/소문자 구분을 제거한다.

a = pd.Series(['hi','hello','haha', 'ha,ha'])
a

[결과]
0       hi
1    hello
2     haha
3    ha,ha
dtype: object

결과 비교

l=['.capitalize()' , '.upper()', '.lower()', '.title()', 
   '.swapcase()', '.casefold()']
for i in l:
    met='a.str'+i
    print(met+' 결과')
    print(eval(met),end='\\n\\n')

[결과]
a.str.capitalize() 결과
0      Haha
1      Haha
2     Ha,ha
3     Ha ha
4    Ha. ha
dtype: object

a.str.upper() 결과
0      HAHA
1      HAHA
2     HA,HA
3     HA HA
4    HA. HA
dtype: object

a.str.lower() 결과
0      haha
1      haha
2     ha,ha
3     ha ha
4    ha. ha
dtype: object

a.str.title() 결과
0      Haha
1      Haha
2     Ha,Ha
3     Ha Ha
4    Ha. Ha
dtype: object

a.str.swapcase() 결과
0      HAHA
1      haha
2     HA,HA
3     HA HA
4    HA. HA
dtype: object

a.str.casefold() 결과
0      haha
1      haha
2     ha,ha
3     ha ha
4    ha. ha
dtype: object

 


 

문자 분류

이부분은 Series의 각 요소에다가 str.메소드를 적용시켜준다고 생각하면 된다.

pandas.Series.str.isalnum()

모든 문자들이 알파벳이나 숫자로 이루어져 있는지 확인

pandas.Series.str.isalpha()

모든 문자들이 알파벳으로 이루어져 있는지 확인

s=pd.Series(['hello',
            'haha',
            '1ha',
            '1',
            ''])
print('str.isalnum() 결과')
print(s.str.isalnum(),end='\\n\\n')
print('str.isalpha() 결과')
print(s.str.isalpha())

[결과]
str.isalnum() 결과
0     True
1     True
2     True
3     True
4    False
dtype: bool

str.isalpha() 결과
0     True
1     True
2    False
3    False
4    False
dtype: bool

pandas.Series.str.isdecimal()

모든 문자열이 숫자(10진수로 표현된)로 이루어져 있는지 확인

pandas.Series.str.isdigit()

모든 문자열이 숫자(isnumeric() + 유니코드에서 윗첨자, 아래첨자)로 이루어져 있는지 확인

pandas.Series.str.isnumeric()

모든 문자들이 숫자(isdigit()+ 유니코드에서 분수를 나타낼 수 있는 문자)로 이루어져 있는지 확인

s = pd.Series(['23', '³', '⅕', '0', ''])
print('str.isdecimal() 결과')
print(s.str.isdecimal(), end='\\n\\n')
print('str.isdigit() 결과')
print(s.str.isdigit(),end='\\n\\n')
print('str.isnumeric() 결과')
print(s.str.isnumeric())

[결과]
str.isdecimal() 결과
0     True
1    False
2    False
3     True
4    False
dtype: bool

str.isdigit() 결과
0     True
1     True
2    False
3     True
4    False
dtype: bool

str.isnumeric() 결과
0     True
1     True
2     True
3     True
4    False
dtype: bool

pandas.Series.str.isspace()

모든 문자열이 공백으로 이루어져 있는지 확인

pandas.Series.str.islower()

모든 문자열이 알파벳 소문자로 이루어져 있는지 확인

pandas.Series.str.isupper()

모든 문자열이 알파벳 대문자로 이루어져 있는지 확인

pandas.Series.str.istitle()

모든 문자열이 titlecase인지 확인

s = pd.Series([' \\n \\t','hi,hi', 'HI,HI', 'Hi,Hi'])
print('str.isspace() 결과')
print(s.str.isspace(), end='\\n\\n')
print('str.islower() 결과')
print(s.str.islower(),end='\\n\\n')
print('str.isupper() 결과')
print(s.str.isupper(), end='\\n\\n')
print('str.istitle() 결과')
print(s.str.istitle())

[결과]
str.isspace() 결과
0     True
1    False
2    False
3    False
dtype: bool

str.islower() 결과
0    False
1     True
2    False
3    False
dtype: bool

str.isupper() 결과
0    False
1    False
2     True
3    False
dtype: bool

str.istitle() 결과
0    False
1    False
2    False
3     True
dtype: bool

'코딩 > 판다승(판다스공부하는희승)' 카테고리의 다른 글

top_25_pandas_tricks(2)  (0) 2022.08.03
Top 25 pandas tricks(1)  (0) 2022.08.02