Changing the type of a DataFrame's column in Pandas
Start your free 7-days trial now!
To change the data type of a DataFrame's column in Pandas, use the Series' astype(~)
method.
Changing type to float
Consider the following DataFrame:
df
A B0 3 51 4 6
To change the data type of column A
to float
:
To confirm that column A
is now of type float
:
df.dtypes
A float64B int64dtype: object
Changing type to string
Consider the following DataFrame:
df
A0 31 4
To convert column A
to type string
:
Dedicated string type
Prior to version 1.0.0, Pandas did not have a data type dedicated to strings. For instance, suppose we created a DataFrame with a column containing just strings:
You'd expect the column type to be string
, but instead you get object
. For backward compatibility, the default type of string-based columns is object
even after version 1.
Pandas officially recommend the string
type over object
type for better:
readability - text should inherently be
string
and not anobject
.performance gains (coming soon)
compatibility with methods like
select_dtypes(~)
where you can specifically pick string-typed columns.
Note that using str
as the argument would convert the type to object
instead of string
:
Changing type to category
Consider the following DataFrame:
df
group0 A1 B
To convert group
column to type category
: