ㅋㄷㅋㄷ Smelt Roe Sushi
728x90

전체 글 274

[Python] 컬럼 타입 변경하기

1) 전체 컬럼 데이터 타입 바꾸기 df = df.apply(pd.to_numeric) 2) 일부 컬럼 데이터 타입 바꾸기 # (1) pd.to_numeric() col = ['나이', '키(cm)', '몸무게'] df[col] = df[col].apply(pd.to_numeric, errors = 'coerce') # 문자열이 포함된 경우 NaN으로 변환하게 함으로써 ValueError 무시하기 # (2) astype() df = df.astype({"나이" : "int", "키(cm)" : "float"}) 3) 한 개 컬럼 데이터 타입 바꾸기 df["나이"] = df["나이"].astype(int)

[Python] 컬럼명 변경하기

1. 컬럼명 전체 수정 df.columns = ["이름", "나이", "주소"] 2. 컬럼명 부분수정 1 df.rename(columns={"A" : "이름"}, inplace = True) # False이면 아무 변화 없음 3. 컬럼명 부분수정 2 df.columns.value[0] = "이름" 4. 컬럼명 부분수정 3 df.rename(columns = {df.columns[0] : "이름"}, inplace = True) # False이면 아무 변화 없음 개인적으로는 1번과 2번을 가장 많이 사용합니다~