Updated April 3, 2023
Introduction to Pandas DataFrame.astype()
Casting is the process of converting entity of one data type into a different data type. So when a entity like object or variable gets casted it will be transformed from its source type to a different type. In pandas this conversion process can be achieved by means of the astype() method. So the astype() method is used to cast a object in the pandas to a different data type. The conversion of the categorical type can also be achieved from one specific column type. In this article will see about Pandas DataFrame.astype().
Syntax and Parameters
Following is a syntax:
Syntax:
DataFrame.astype(self: ~FrameOrSeries, dtype, copy: bool = True, errors: str = 'raise')
Following are the different parameters with description:
Parameter | Description |
Dtype | The data type to be converted will be mentioned here.
The python type or a numpy.dtype can be mentioned here which will make the whole object to be of one specific object type. Additionally {col: dtype.. . . . . . . . . } can be used to convert one or more columns of the object to some specific type. |
Copy | A complete copy of the entire source object will be performed when the copy value is set to true. ( Need to be exceptionally cautious while setting the value of copy as False as alteration to values then may disseminate to other pandas objects ). |
Errors | Represents whether an exception needs to be raised or not. Majorly this option allows to control whether a exception has to be raised or not on a case where a exception could be validly occurring. ( Need to be exceptionally cautious while setting the value of error parameter is been raise or ignore ).
|
Examples of Pandas DataFrame.astype()
Following are the examples as given below:
Example #1
Code:
import pandas as pd
Core_Series = pd.Series([ 10, 20, 30, 40, 50, 60])
print(" THE CORE SERIES ")
print(Core_Series)
Transformed_Series = Core_Series.astype('float32')
print("")
print(" THE FLOAT SERIES ")
print(Transformed_Series)
Output:
Code Explanation: Here the pandas library is initially imported and the imported library is used for creating a series. The values in the series are formulated in such way that they are a series of 10 to 60. the astype() method is used to convert the values of the series from int type to a float type. So we can notice from the output snap that the entire series of int type gets transformed into a float type. the contents of the dataframe before and after the transformation are been printed on to the console. We can clearely notice that the float series which was formulated has all its values in float format because of the casting process which was applied.
Example #2
Code:
import pandas as pd
Core_Dataframe = pd.DataFrame({'A' : [ 1, 6, 11, 15, 21, 26],
'B' : [2, 7, 12, 17, 22, 27],
'C' : [3, 8, 13, 18, 23, 28],
'D' : [4, 9, 14, 19, 24, 29],
'E' : [5, 10, 15, 20, 25, 30]})
print(" THE CORE DATAFRAME ")
print(Core_Dataframe)
print(type(Core_Dataframe.A[0]))
Transformed_Dataframe = Core_Dataframe.astype('float32')
print("")
print(" THE FLOAT DATAFRAME ")
print(Transformed_Dataframe)
print(type(Transformed_Dataframe.A[0]))
Output:
Code Explanation: Here the pandas library is initially imported and the imported library is used for creating the dataframe which is a shape(6,6). all of the columns in the dataframe are assigned with headers which are alphabetic. the values in the dataframe are formulated in such way that they are a series of 1 to n. Here the data frame created is notified as core dataframe. the datatype of the core dataframe is printed on to the console. Next, the astype() method is used to convert the entire dataframe into a float type from a int type element. so at the end of astype() process the entire core dataframe is converted into a float type and named as transformed dataframe. the contents of the transformed datatype is printed onto the console along with the type of it.
Example #3
Code:
import pandas as pd
Core_Dataframe = pd.DataFrame({'A' : [ 1, 6, 11, 15, 21, 26],
'B' : [2, 7, 12, 17, 22, 27],
'C' : [3, 8, 13, 18, 23, 28],
'D' : [4, 9, 14, 19, 24, 29],
'E' : [5, 10, 15, 20, 25, 30]})
print(" THE CORE DATAFRAME ")
print(Core_Dataframe)
print(type(Core_Dataframe.A[0]))
print(type(Core_Dataframe.B[0]))
print(type(Core_Dataframe.C[0]))
print(type(Core_Dataframe.D[0]))
print(type(Core_Dataframe.E[0]))
Transformed_Dataframe_A = Core_Dataframe['A'].astype('float32')
Transformed_Dataframe_B = Core_Dataframe['B'].astype('str')
Transformed_Dataframe_C = Core_Dataframe['C'].astype('float64')
Transformed_Dataframe_D = Core_Dataframe['D'].astype('int32')
Transformed_Dataframe_E = Core_Dataframe['E'].astype('complex64')
print("")
print(" THE TRANSFORMED DATAFRAME TYPES ")
print(" THE TRANSFORMED DATAFRAME A OF FLOAT32 TYPE ")
print(Transformed_Dataframe_A)
print(type(Transformed_Dataframe_A[0]))
print("")
print(" THE TRANSFORMED DATAFRAME A OF STR TYPE ")
print(Transformed_Dataframe_B)
print(type(Transformed_Dataframe_B[0]))
print("")
print(" THE TRANSFORMED DATAFRAME A OF FLOAT64 TYPE ")
print(Transformed_Dataframe_C)
print(type(Transformed_Dataframe_C[0]))
print("")
print(" THE TRANSFORMED DATAFRAME A OF INT32 TYPE ")
print(Transformed_Dataframe_D)
print(type(Transformed_Dataframe_D[0]))
print("")
print(" THE TRANSFORMED DATAFRAME A OF COMPLEX64 TYPE ")
print(Transformed_Dataframe_E)
print(type(Transformed_Dataframe_E[0]))
Output:
Code Explanation: The whole initial set of operations from the above example are repeated here again , Once the core dataframe is been declared the datatype of each of the columns in the dataframe are printed into the console encapsulated by the type function , so the type value of each of the column is printed on to the console. Each of the columns in the console are casted to a different type and stored into a transformed dataframe independently, the type values casted for each column is printed below,
- Core_Dataframe[‘A’] is transformed into float32
- Core_Dataframe[‘B’] is transformed into string
- Core_Dataframe[‘C’] is transformed into float64
- Core_Dataframe[‘D’] is transformed into int32
- Core_Dataframe[‘E’] is transformed into complex64
The values and the corresponding datatypes of the values for each and every transformed dataframe is printed onto the console.
Conclusion
The astype() method in pandas shows the flexibility of applying a casting operation over each and every value in the dataframe in a most flexible way. It also depicts the classified set of cast types which can be associated to astype() method of python pandas programming.
Recommended Articles
We hope that this EDUCBA information on “Pandas DataFrame.astype()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.