float를 int로 변환한 결과인 데이터 프레임이 있다고 가정합니다.
Before conversion Name object Age int64 Maths int64 Science int64 English int64 Result float64 dtype: object After conversion Name object Age int64 Maths int64 Science int64 English int64 Result int64 dtype: object
이 문제를 해결하기 위해 다음 단계를 따릅니다. -
해결책
-
데이터 프레임 정의
-
float 데이터 유형 열 '결과'를 다음과 같이 'int'로 변환 -
df.Result.astype(int)
예시
더 나은 이해를 위해 아래 구현을 살펴보겠습니다. −
import pandas as pd
data = {'Name': ['David', 'Adam', 'Bob', 'Alex', 'Serina'],
'Age' : [13,12,12,13,12],
'Maths': [98, 59, 66, 95, 70],
'Science': [75, 96, 55, 49, 78],
'English': [79, 45, 70, 60, 80],
'Result': [8.1,6.2,6.3,7.2,8.3]}
df = pd.DataFrame(data)
print("Before conversion\n", df.dtypes)
df.Result = df.Result.astype(int)
print("After conversion\n",df.dtypes) 출력
Name object Age int64 Maths int64 Science int64 English int64 Result float64 dtype: object Name object Age int64 Maths int64 Science int64 English int64 Result int64 dtype: object