Python | Pandas DataFrame.astype ()

DataFrame.astype() is used to cast the pandas object to the specified dtype.  astype () also provides the ability to convert any suitable existing column to a categorical type.

DataFrame.astype() is very handy when we want to DataFrame.astype() a specific datatype of a column to a different datatype. Not only that, but we can also use Python dictionary input to change more than one column type at a time. The label of the key in the dictionary matches the column name, and the label of the values ​​in the dictionary matches the new datatypes we want the columns to be from.

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 a numpy.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

To link to the CSV file used in the code, click here

Example # 1: Convert the data type of the Weight column.

# import pandas as pd

import pandas as pd

 
# Create data frame from CSV file

df = pd.read_csv ( " nba.csv " )

 
# Print the first 10 lines
# data frame for rendering

 

df [: 10 ]

Since the data has some” nan “values, in order to avoid any error, we will discard all lines containing any nan values ​​.

# discard all lines that
# is & # 39; nan & # 39 ;.

df.dropna (inplace = True )

# let`s figure out the data type of the Weight column

before = type (df.Weight [ 0 ])

 
# We are now converting it to int64.

df.Weight = df.Weight.astype ( `int64` )

 
# let`s figure out the data type after casting

after = type (df.W eight [ 0 ])

 
# print value before
before

 
# output value after
after

Output:

# print the data frame and see
# how it looks after the change
df

Example # 2: change the data type of more than one column at a time

Change the Name column to categorical type and Age column to int64 type.

# import pandas as pd

import pandas as pd

 
# Create data frame from CSV file

df = pd.read_csv ( "nba.csv" )

 
# Delete lines with values ​​& # 39; nan & # 39;

df = df.dropna ()

  
# print the existing data type of each column
df.info ()

Output:

Now let`s change both column data types at the same time.

# Passed the dictionary into the astype () function

df = df.astype ({ "Name" : ` category` , "Age" : `int64` })

  
# Now print datatype
# of all columns after modification
df.info ()

Output:

# print data frame
# also after the change
df

Output: