본문 바로가기
개발 Tools/파이썬_Pandas & Numpy

파이썬 Pandas rename (행열 이름 변경)

by 전컴반 2023. 2. 21.
반응형
rename

 

index, column의 이름을 딕셔너리 형태로 묶어서 바꾼다.

 

이때 inplace=Ture를 지정해야 변경된다. inplace를 지정하지 않은 index는 바뀌지 않았다. 

 

import pandas as pd

df = pd.DataFrame([[0, 1, 2], [3, 4, 5]], index=["r0", "r1"], columns=["c0", "c1", "c2"])
print(df)

출력
    c0  c1  c2
r0   0   1   2
r1   3   4   5

 

각각의 행과 열의 이름을 바꿔준다. inplace=True를 해줘야 한다. 안해주면 바뀌지 않는다.

아니면, df = df.rename() 요로콤하면 된다.

 

df.rename(columns={"c0": "col0", "c1": "col1", "c2": "col2"}, inplace=True)
df.rename(index={"r0": "row0", "r1": "row1"})
print(df)

출력
    col0  col1  col2
r0     0     1     2
r1     3     4     5
반응형

댓글