Change language

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:

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically