df.drop(['col1', 'col2'], axis=1, inplace=True) # 여러 열 삭제
df.drop([0, 1, 2], axis=0, inplace=True) # 인덱스 기준 삭제
df.dropna(subset=['col1'], inplace=True) # 결측치 있는 행 삭제
df.reset_index(drop=True, inplace=True) # 인덱스 초기화
df.set_index('id', inplace=True) # 특정 컬럼을 인덱스로
df.rename(columns={'old': 'new'}, inplace=True)
# 전체 컬럼명 일괄 변경
df.columns = ['name', 'age', 'gender']
df['BMI'] = df['weight'] / (df['height'] / 100) ** 2 # 계산해서 생성
df['age_group'] = df['age'].apply(lambda x: '청년' if x < 30 else '성인')
df = df[['id', 'name', 'age']] # 원하는 순서로 재정렬
실수 | 예시 | 해결 방법 |
---|---|---|
axis 방향 실수 | axis=0(행), axis=1(열) 헷갈림 | 축 개념 익히기! |
컬럼 이름에 공백 있음 | ' 이름 ' 처럼 | df.columns.str.strip() |
인덱스 초기화 안 함 | drop 후 인덱스가 엉망 | reset_index(drop=True) |
열 삭제 후 원본에 반영 안 됨 | inplace=True 빼먹음 | 꼭 명시하거나 새 변수에 저장 |
불필요한 열/행을 삭제했는가?
컬럼명은 읽기 쉽게 바꿨는가?
인덱스는 논리적으로 정리되었는가?
필요한 파생변수(새 열)를 추가했는가?
열의 순서가 직관적으로 정리되었는가?