Pandas DataFrame astype 메서드

Beginner

소개

이 랩에서는 Python 의 Pandas 라이브러리에서 astype() 메서드를 사용하는 방법을 배웁니다. astype() 메서드를 사용하면 Pandas DataFrame 의 데이터 유형을 지정된 유형으로 변환할 수 있습니다. DataFrame 의 모든 열 또는 특정 열의 데이터 유형을 변환할 수 있습니다.

VM 팁

VM 시작이 완료되면 왼쪽 상단 모서리를 클릭하여 Notebook 탭으로 전환하여 실습을 위해 Jupyter Notebook에 액세스하십시오.

때로는 Jupyter Notebook 이 로딩을 완료하는 데 몇 초 정도 기다려야 할 수 있습니다. Jupyter Notebook 의 제한 사항으로 인해 작업의 유효성 검사는 자동화할 수 없습니다.

학습 중에 문제가 발생하면 언제든지 Labby 에게 문의하십시오. 세션 후 피드백을 제공해주시면 문제를 즉시 해결해 드리겠습니다.

DataFrame 의 모든 열 데이터 유형 변환

astype() 메서드를 사용하여 DataFrame 의 모든 열 데이터 유형을 지정된 유형으로 변환할 수 있습니다. 다음은 예시입니다.

import pandas as pd

## Create a DataFrame
data = {'A':[1,2,3,4,5],'B':[6,7,8,9,10]}
df = pd.DataFrame(data)

print("----Before converting datatype of DataFrame-----")
print(df.dtypes)

## Convert the datatypes of all columns to int32
df = df.astype('int32')

print("----After converting datatype of DataFrame-----")
print(df.dtypes)

출력:

----Before converting datatype of DataFrame-----
A int64
B int64
dtype: object

----After converting datatype of DataFrame-----
A int32
B int32
dtype: object

이 예제에서는 AB 두 개의 열이 있는 DataFrame 을 생성합니다. 데이터 유형을 변환하기 전에 두 열 모두 int64 데이터 유형을 가지고 있습니다. 인수로 'int32'를 사용하여 astype() 메서드를 사용한 후, 두 열 모두 int32 데이터 유형으로 변환됩니다.

DataFrame 의 특정 열 데이터 유형 변환

astype() 메서드를 사용하여 DataFrame 의 특정 열의 데이터 유형을 변환할 수도 있습니다. 다음은 예시입니다.

import pandas as pd

## Create a DataFrame
data = {'A':[1,2,3,4,5],'B':[6,7,8,9,10]}
df = pd.DataFrame(data)

print("----Before converting datatype of DataFrame-----")
print(df.dtypes)

## Convert the datatype of column 'A' to int32
df['A'] = df['A'].astype('int32')

print("----After converting single column datatype of DataFrame-----")
print(df.dtypes)

출력:

----Before converting datatype of DataFrame-----
A int64
B int64
dtype: object

----After converting single column datatype of DataFrame-----
A int32
B int64
dtype: object

이 예제에서는 AB 두 개의 열이 있는 DataFrame 을 생성합니다. 데이터 유형을 변환하기 전에 열 A는 int64 데이터 유형을 가지고 있고 열 B는 int64 데이터 유형을 가지고 있습니다. 열 A에 대해 인수로 'int32'를 사용하여 astype() 메서드를 사용한 후, 열 A의 데이터 유형만 int32 로 변환되고 열 B의 데이터 유형은 int64 로 유지됩니다.

변환된 DataFrame 확인

DataFrame 의 데이터 유형을 변환한 후, 업데이트된 DataFrame 을 확인하여 변환이 성공적으로 이루어졌는지 확인할 수 있습니다. 다음은 예시입니다.

import pandas as pd

## Create a DataFrame
data = {'A':[1,2,3,4,5],'B':[6,7,8,9,10]}
df = pd.DataFrame(data)

print("----After converting single column datatype of DataFrame-----")
df['B'] = df['B'].astype('float')
print(df.dtypes)
print("-----DataFrame after converting to float datatypes-----")
print(df)

출력:

----After converting single column datatype of DataFrame-----
A int64
B float64
dtype: object

-----DataFrame after converting to float datatypes-----
   A     B
0  1   6.0
1  2   7.0
2  3   8.0
3  4   9.0
4  5  10.0

이 예제에서는 AB 두 개의 열이 있는 DataFrame 을 생성합니다. 열 B에 대해 인수로 'float'를 사용하여 astype() 메서드를 사용한 후, 열 B의 데이터 유형은 float64 로 변환됩니다. 그런 다음 변경 사항을 확인하기 위해 DataFrame 의 데이터 유형을 출력하고, 업데이트된 값을 확인하기 위해 DataFrame 자체를 출력합니다.

요약

이 랩에서는 Pandas 에서 astype() 메서드를 사용하여 DataFrame 의 데이터 유형을 변환하는 방법을 배웠습니다. astype() 메서드를 사용하여 모든 열 데이터 유형과 특정 열 데이터 유형을 변환하는 방법과 변환 후 결과 DataFrame 을 확인하는 방법을 살펴보았습니다. astype() 메서드는 Pandas DataFrame 에서 데이터를 조작하고 변환하는 데 유용합니다.