본문 바로가기
프로그래밍/Python

[Pandas / Dataframe]특정 컬럼의 특정 문자열을 변경하는 코드 / 변경이 안될때 해결법

by Mr.noobiest 2024. 1. 19.

python pandas

 

 

Dataframe의 특정 문자 변경


아래와 같은 dataframe 'df'가 있다고 가정할때, "col2" 컬럼의 "test_"를 ""로 변경하려고 한다.

df

col1 col2 col3
test_col1 test_col2_1 test_col3_1
test_col2 test_col2_2 test_col3_2
test_col3 test_col2_3 test_col3_3
test_col4 test_col2_4 test_col3_4

코드는 아래와 같습니다.

 

 

Python Replace Code

df['col2']=df['col2'].str.replace('test_','')

 

str을 안붙여주면 에러는 안뜨는데 변경이 제대로 안되니 주의할것

 

끝.

728x90
반응형