Python/Python For Analytics

[Python] Pandas DataFrame 컬럼명 특정 문자로 변경

Pydole 2019. 9. 25. 16:04

 

import pandas as pd


df = pd.DataFrame({'A-1':[1,2,3,4,5],'A-2':[1,2,3,4,5]})
print(df.columns)
---------------------------------------------------------
Index(['A-1', 'A-2'], dtype='object')



# columns.str.replace(변경전 문자,변경할 문자)



df.columns = df.columns.str.replace('-','_')
print(df.columns)
---------------------------------------------------------
Index(['A_1', 'A_2'], dtype='object')


# - A-1이 A_1로 컬럼명이 변경