Python | Pandas DataFrame.astype() (original) (raw)
Last Updated : 03 Dec, 2023
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
DataFrame.astype()
method is used to cast a pandas object to a specified dtype.astype()
function also provides the capability to convert any suitable existing column to a categorical type.
DataFrame.astype()
function comes in very handy when we want to compare a particular column data type to another data type. Not only that but we can also use a Python dictionary input to change more than one column type at once. The key label in the dictionary is corresponding to the column name and the values label in the dictionary corresponds to the new data types we want the columns to be of.
Pandas DataFrame.astype() Syntax
**Syntax: _DataFrame.astype(dtype, copy=True, errors=’raise’, **kwargs)
**Parameters:
- **dtype : Use a
numpy.dtype
or Python type to cast entire pandas object to the same type. Alternatively, use {col: dtype, …}, where col is a column label and dtype is anumpy.dtype
or Python type to cast one or more of the DataFrame’s columns to column-specific types.- **copy : Return a copy when copy=True (be very careful setting copy=False as changes to values then may propagate to other pandas objects).
- **errors : Control raising of exceptions on invalid data for provided dtype.
- **raise : allow exceptions to be raised
- **ignore : suppress exceptions. On error return original object
- ****kwargs :**keyword arguments to pass on to the constructor
**Returns: casted : type of caller
For link to CSV file Used in Code, click here
Python Pandas DataFrame.astype() Function Examples
Below are some examples of Pandas DataFrame.astype() Function:
**Example 1: Convert the Weight Column Data Type
In this example, the code uses pandas to read data from a CSV file named “nba.csv” into a DataFrame and prints the first 10 rows for initial data exploration.
Python3
import
pandas as pd
df
=
pd.read_csv(
"nba.csv"
)
df[:
10
]
As the data have some “nan” values so, to avoid any error we will drop all the rows containing any nan
values.
Python3
Here, the variable before
stores the data type of the first element in the ‘Weight’ column before conversion, and after
stores the data type after converting the entire ‘Weight’ column to ‘int64’. The values of before
and after
are not explicitly printed, so you may want to use print(before)
and print(after)
to display the results.
Python3
before
=
type
(df.Weight[
0
])
df.Weight
=
df.Weight.astype(
'int64'
)
after
=
type
(df.Weight[
0
])
before
after
**Output:
We will now print the DataFrame.
Python3
**Example 2: Change the Data Type of More than One Column at Once
Change the Name
column to categorical type and Age
column to int64 type. In this example, the code uses pandas to read a CSV file named “nba.csv” into a DataFrame (df
). It then drops rows containing ‘nan’ values, and df.info()
is used to display the existing data types of each column in the cleaned DataFrame.
Python3
import
pandas as pd
df
=
pd.read_csv(
"nba.csv"
)
df
=
df.dropna()
df.info()
**Output:
Now let’s change both the columns data type at once.
Python3
df
=
df.astype({
"Name"
:
'category'
,
"Age"
:
'int64'
})
df.info()
**Output:
Now, we will print the DataFrame.
Python3
**Output: