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 anumpy.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 objectkwargs: 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.
|
Since the data has some" nan "values, in order to avoid any error, we will discard all lines containing any nan values
.
|
|
Output:
|
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.
|
Output:
Now let’s change both column data types at the same time.
|
Output:
|
Output: