LiJell's 성장기

10.python_replace_method 본문

Bigdata/파이썬_Python

10.python_replace_method

All_is_LiJell 2022. 1. 3. 20:02
반응형

Modified on Jan 03 2022
# -- coding: utf-8 --
"""
Created on Fri Dec 24 11:06:32 2021

@author: hanju
"""

replace 메서드_치환, 삭제

replace 메서드

  • replce(찾을 문자열, 바꿀 문자열)

1. 기본 문자열 메서드

  • 기본 파이썬 제공
  • 문자열에서 호출 가능
  • 벡터 연산(각 원소별 반복 처리) 불가
  • 오직 문자열 치환만 가능(숫자치환, NA 치환 불가능)
'abcd'.replace('a', 'A') #'Abcd' >> a를 A로 치환
'abcd'.replace('a', '')  # 'bcd' >> 공백으로 치환
'abcd1'.replace(1, '')   # string이 아니여서 숫자를 찾아서 치환하는 거 안됨
# TypeError: replace() argument 1 must be str, not int
' bcd1'.replace('', 1)   # 숫자로 치환하는 거 안됨
# TypeError: replace() argument 2 must be str, not int

['abcd','abcde','aabb'].replace(',','')
# AttributeError: 'list' object has no attribute 'replace'
# list 는 replace 호출 불가

for 문 활용

outlist = []
for i in ['abcd','abcde','aabb']:
    outlist.append(i.replace('a','A'))

print(outlist)    

# ['Abcd', 'Abcde', 'AAbb']

map 함수

f1 = lambda x : x.replace('a','A')

list(map(f1, ['abcd','abcde','aabb']))
# ['Abcd', 'Abcde', 'AAbb']

['abcd','abcde','aabb'].map(f1)
# 'list' object has no attribute 'map'
# list 객체는 map 함수 호출 불가

from pandas import Series, DataFrame
['abcd','abcde','aabb'].map(f1) # 호출 불가
Series(['abcd','abcde','aabb']).map(f1) # Series는 호출 가능(method chaining)

# 0     Abcd
# 1    Abcde
# 2     AAbb
# dtype: object

import numpy as np
Series(['abcd','abcde','aaab',np.nan]).map(lambda x : x.replace(np.nan, ''))
# TypeError: replace() argument 1 must be str, not float
# np.nan은 int로 인식하기 때문에 오류, str 이어야 함

2.str.replace

  • pandas 제공(Series 객체만 호출 가능)
  • 백터화 내장된 문자열 메서드
  • 문자열 호출 가능
  • 백터 연산 (각 원소별 반복 처리) 가능
  • 오직 문자열 치환만 가능 (숫자 치환, NA 치환 불가)
  • 숫자로만 구성된 Series 적용 불가
a = ['abcd','abcde','aabb']
Series(a).str.replace('a','A')
# 0     Abcd
# 1    Abcde
# 2     AAbb
# dtype: object

a.str.replace('a','A')

# AttributeError: 'list' object has no attribute 'str'
# 리스트는 호출 불가

DataFrame(a).str.replace('a','A')
# AttributeError: 'DataFrame' object has no attribute 'str'
# 데이터 프레임도 안됨

pandas replace

  • pandas 제공
  • 값 치환 메서드 (똑같은, 완전히 일치하는 값을 다른 값으로 대체, 삭제)
  • Series, DataFrame 호출 가능
  • 숫자, NaN 치환 가능
  • 동시에 여러 대상 수정 가능
df1 = DataFrame([['1,200','1,300'],['1,400','1,500']])
df1

#        0      1
# 0  1,200  1,300
# 1  1,400  1,500

df1.replace(',','') # what? 변화 없음 ',' 로 생긴 값을 삭제하는 의미

#        0      1
# 0  1,200  1,300
# 1  1,400  1,500

df2 = DataFrame([['1,200',','],['1,400','1,500']])
df2

#        0      1
# 0  1,200      ,
# 1  1,400  1,500

df2.replace(',','') # 완전히 일치하는 값을 대체하기 때문에, df1에서는 ','가 없어서 인식 안됨
#        0      1
# 0  1,200       
# 1  1,400  1,500

df3 = DataFrame([['1,200','1,300'],['1,400','.']], columns = ['a','b'])
df3
#        a      b
# 0  1,200  1,300
# 1  1,400      .

df3.replace('.', np.nan) # '.' 와 완전히 일치하는 값은 NaN 치환

#        a      b
# 0  1,200  1,300
# 1  1,400    NaN

df3.replace(['.','1,400','?','!'], np.nan)
#        a      b
# 0  1,200  1,300
# 1    NaN    NaN

[연습 문제]

  • df1에 천단위 구분기호 제거 후 모두 숫자 변경
df1.applymap(lambda x: int(x.replace(',','')))

# scala 에서는 문자열이 아닌 숫자로 변경해야 함

#       0     1
# 0  1200  1300
# 1  1400  1500

df1.applymap(lambda x: int(x.replace(',',''))).sum()
# 0    2600
# 1    2800
# dtype: int64

# 동료 코드
copy_df1 = df1
for i in range(0,len(df1)):
    copy_df1[i] = df1[i].str.replace(',','')
    print(copy_df1)


#다른분

for i in range(len(df1)):
    if df1[i].all() != 'int':
        df1[i] = df1[i].str.replace(',','').astype('int')
    else:
        pass
print(df1)
반응형

'Bigdata > 파이썬_Python' 카테고리의 다른 글

12.python_pandas_groupby  (0) 2022.01.05
11.python_pandas_sort  (0) 2022.01.04
09.python_pandas_arithmetic_method  (0) 2022.01.02
08.python_pandas_string_method  (0) 2022.01.02
07. Pandas _ Series, DataFrame  (0) 2022.01.01
Comments