0Pricing
Pandas & NumPy Academy · 강의

astype()으로 형 변환하기

astype()을 사용해 열을 정수, 실수, 문자열, 불리언, 날짜시간 형식으로 변환하고 일반적인 변환 오류를 처리합니다.

astype()으로 형 변환하기은(는) CoddyKit의 무료 Pandas & NumPy Academy 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Pandas & NumPy Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Pandas & NumPy Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Introduction to astype()

Series.astype(dtype) converts a column from its current type to the type you specify. It returns a new Series (or DataFrame when called on a full DataFrame) without modifying the original. This is the primary Pandas tool for fixing dtype problems after loading data, and it can convert between numeric types, to strings, to booleans, and to the Categorical type.

import pandas as pd

df = pd.DataFrame({'age': ['25', '30', '35']})
print('Before:', df['age'].dtype)  # object

df['age'] = df['age'].astype(int)
print('After:', df['age'].dtype)   # int64
print(df['age'] + 1)               # [26, 31, 36] — arithmetic now works

Converting to Numeric Types

The most common conversion is from object to a numeric type. You can cast to 'int64', 'int32', 'float64', 'float32', etc. If any value in the column cannot be converted, astype() raises a ValueError. Use pd.to_numeric(series, errors='coerce') instead when the column may contain non-numeric strings — it converts bad values to NaN rather than crashing.

import pandas as pd

df = pd.DataFrame({'price': ['10.5', '20.0', 'N/A', '30.5']})

# astype would crash on 'N/A'
# price_float = df['price'].astype(float)  # ValueError!

# pd.to_numeric with errors='coerce' is safer
df['price_float'] = pd.to_numeric(df['price'], errors='coerce')
print(df)
#   price  price_float
# 0  10.5         10.5
# 1  20.0         20.0
# 2   N/A          NaN
# 3  30.5         30.5

Converting to String (object)

Casting a column to str (or 'object') converts every value to its string representation. This is useful when you need to concatenate a numeric column with a text column, or when you want to apply string methods to numeric data like zero-padding IDs. Pandas also has the newer 'string' dtype that offers better NA-handling for text columns.

import pandas as pd

df = pd.DataFrame({'id': [1, 2, 3], 'region': ['E', 'W', 'N']})

# Concatenate id and region into a composite key
df['key'] = df['id'].astype(str) + '_' + df['region']
print(df)
#    id region  key
# 0   1      E  1_E
# 1   2      W  2_W
# 2   3      N  3_N

Converting to Boolean

Boolean conversion is useful when columns store True/False as integers (0/1) or as strings ('Yes'/'No'). Casting 0/1 integers with astype(bool) works directly: 0 becomes False, anything non-zero becomes True. For string 'Yes'/'No' columns, map first with a dictionary, then cast.

import pandas as pd

df = pd.DataFrame({
    'active_int': [1, 0, 1, 0],
    'active_str': ['Yes', 'No', 'Yes', 'No']
})

# Integer to bool
df['active_bool'] = df['active_int'].astype(bool)

# String to bool via mapping
df['active_bool2'] = df['active_str'].map({'Yes': True, 'No': False})

print(df[['active_bool', 'active_bool2']])
#    active_bool  active_bool2
# 0         True          True
# 1        False         False

Downcasting Numeric Types

By default, Pandas uses 64-bit types. If your integer values fit in 32 or even 8 bits, you can downcast to a smaller type to halve (or quarter) memory usage. Use pd.to_numeric(series, downcast='integer') or explicitly cast with astype('int32'). This is a key optimisation for large datasets.

import pandas as pd
import numpy as np

df = pd.DataFrame({'count': np.random.randint(0, 200, 1_000_000)})

print('int64 bytes:', df['count'].nbytes)   # 8,000,000

df['count_int16'] = df['count'].astype('int16')  # max 32767, fits 0-200
print('int16 bytes:', df['count_int16'].nbytes)  # 2,000,000

# Or let Pandas choose the smallest type automatically
df['count_auto'] = pd.to_numeric(df['count'], downcast='integer')
print('auto dtype:', df['count_auto'].dtype)

Converting Full DataFrames with astype(dict)

You can convert multiple columns at once by passing a dictionary to df.astype() where keys are column names and values are target dtypes. This is cleaner than chaining individual column assignments and makes the type-conversion step self-documenting as a single block in your pipeline.

import pandas as pd

df = pd.DataFrame({
    'age': ['25', '30'],
    'salary': ['50000', '70000'],
    'is_manager': ['1', '0']
})

df = df.astype({
    'age': 'int32',
    'salary': 'float64',
    'is_manager': 'bool'
})
print(df.dtypes)
# age           int32
# salary      float64
# is_manager     bool

Handling Errors in astype()

astype() has no built-in error-tolerant mode (unlike pd.to_numeric). If a conversion fails, it raises a ValueError or OverflowError. The safe workflow is: (1) clean the column first (strip symbols, fill NaN), (2) then cast. Alternatively, use pd.to_numeric(errors='coerce') for numerics or write a small helper function that catches conversion errors.

import pandas as pd

# Clean then cast pattern
df = pd.DataFrame({'price': ['$1,200', '$800', '$450']})

# Step 1: remove non-numeric characters
df['price_clean'] = (
    df['price']
    .str.replace('$', '', regex=False)
    .str.replace(',', '', regex=False)
)
# Step 2: safe cast
df['price_num'] = df['price_clean'].astype(float)
print(df)
#      price price_clean  price_num
# 0  $1,200        1200     1200.0
# 1    $800         800      800.0
# 2    $450         450      450.0

Casting to Pandas StringDtype

The newer 'string' dtype (capital S variant or pd.StringDtype()) was introduced to provide first-class string support with proper NA handling — it stores missing strings as pd.NA rather than None or np.nan. It enables the .str accessor while preserving NA semantics better than the object dtype, and is recommended for new code targeting Pandas 2+.

import pandas as pd

df = pd.DataFrame({'name': ['Alice', None, 'Carol']})

# Convert to the modern string dtype
df['name'] = df['name'].astype('string')
print(df['name'].dtype)   # string
print(df['name'].isna())  # proper NA detection
# 0    False
# 1     True
# 2    False

Overflow Risks When Downcasting

When you downcast to a smaller integer type, values that exceed the type's range silently overflow and wrap around. For example, casting 300 to int8 (range -128 to 127) produces 44 without raising an error. Always verify that your actual data range fits within the target type before downcasting to avoid subtle corruption.

import pandas as pd
import numpy as np

df = pd.DataFrame({'value': [100, 200, 300, 127, 128]})

# int8 range: -128 to 127
df['value_i8'] = df['value'].astype('int8')
print(df)
#    value  value_i8
# 0    100       100
# 1    200       -56   <- overflow! 200-256 = -56
# 2    300        44   <- overflow! 300-256 = 44
# 3    127       127
# 4    128      -128   <- overflow!

# Always check range first
print(df['value'].max())  # 300 > 127 — int8 is UNSAFE here

Verifying Conversions with a Dtype Map

After performing multiple type conversions, validate the final schema by comparing the actual dtypes to an expected map. This assertion pattern catches mistakes like a column that failed to convert (e.g., because NaN prevented int conversion). Run these checks at the end of your data loading function as a lightweight schema test.

import pandas as pd

df = pd.DataFrame({
    'user_id': [1, 2, 3],
    'amount': [10.5, 20.0, 30.5],
    'active': [True, False, True]
})

expected = {'user_id': 'int64', 'amount': 'float64', 'active': 'bool'}

for col, dtype in expected.items():
    assert str(df[col].dtype) == dtype, (
        f'{col}: expected {dtype}, got {df[col].dtype}'
    )
print('Schema validation passed')

Full Conversion Pipeline Example

Bringing it all together: here is a realistic post-read_csv conversion pipeline that cleans formatting, handles NaN safely, casts to optimal types, and validates the result. This pattern — clean, cast, validate — should be a standard part of every data ingestion function.

import pandas as pd

raw = pd.DataFrame({
    'user_id': ['101', '102', '103'],
    'revenue': ['$1,200', '$800', '$2,500'],
    'active': ['Yes', 'No', 'Yes']
})

df = (
    raw
    .assign(
        user_id=raw['user_id'].astype('int32'),
        revenue=pd.to_numeric(
            raw['revenue'].str.replace('[$,]', '', regex=True)
        ),
        active=raw['active'].map({'Yes': True, 'No': False})
    )
)
print(df.dtypes)
# user_id      int32
# revenue    float64
# active        bool

Quick Check

Test your understanding of casting with astype().

Lesson Recap

In this lesson you learned: astype(dtype) converts a column or entire DataFrame to a new type, pd.to_numeric(errors='coerce') is safer for columns with non-numeric strings, and passing a dictionary to astype() converts multiple columns at once. Downcast carefully to avoid overflow, and always validate the final schema with assertions. Next up we explore the memory-efficient Categorical dtype.

자주 묻는 질문

“astype()으로 형 변환하기” 강의는 무료인가요?

네 — “astype()으로 형 변환하기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Pandas & NumPy Academy 강의 전체를 잠금 해제할 수 있습니다. Pandas & NumPy Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

“astype()으로 형 변환하기”에서 뭘 배우나요?

astype()을 사용해 열을 정수, 실수, 문자열, 불리언, 날짜시간 형식으로 변환하고 일반적인 변환 오류를 처리합니다. 브라우저에서 직접 실행하는 실습 코드로 Pandas & NumPy Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Pandas & NumPy Academy을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Pandas & NumPy Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.

“astype()으로 형 변환하기” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Pandas & NumPy Academy 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Pandas & NumPy Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 열 데이터 형식 확인하기
  2. astype()으로 형 변환하기
  3. 범주형 데이터 형식
  4. 날짜 올바르게 파싱하기
← Pandas & NumPy Academy(으)로 돌아가기