astype()
noun
say it: “AS-type” /ˈæzˌtaɪp/
A pandas method that changes the type of a column's values, such as from text to whole numbers, and stops if a value can't be changed.
What does astype() do in pandas?
astype() is the pandas method that turns a column into another type, such as text that holds whole numbers into
real numbers, or a few repeated labels into "category". It stops with an error at the first value it can't convert,
such as "59 lbs", because it won't guess which part is the number.
import pandas as pd
kids = pd.Series(["3", "2", "5"]) # text, so sum() glues it: "325"
print(kids.astype(int).sum()) # 10
In a sentence
- “astype() stopped at "59 lbs", because it couldn't tell which part was the number.”
- “After astype(), the ages column added up instead of gluing the digits together.”
Level: Journeyman · More Python terms at this level
Updated