How to Convert to Best Data Types Automatically in Pandas? - GeeksforGeeks

02 Dec, 2020
Prerequisite: Pandas
In pandas datatype by default are int, float and objects. When we load or create any series or dataframe in pandas, pandas by default assigns the necessary datatype to columns and series. 
We will use pandas convert_dtypes() function to convert the default assigned data-types to the best datatype automatically. There is one big benefit of using convert_dtypes()- it supports new type for missing values pd.NA along with NaN. It is supported in pandas 1.1.4 version.
Syntax:
For Series:
series_name.convert_dtypes()
For DataFrame:
dataframe_name.convert_dtypes().dtypes
The following is the implementation for both series and data frame:
Converting the datatype of a series:
Import module
Create a series
Now use convert_dtypes() function to automatically convert datatype
Example:
Python3
# importing packagesimport pandas as pd  # creating a seriess = pd.Series(['Geeks', 'for', 'Geeks'])  # printing the seriesprint("SERIES")print(s)  print()  # using convert_dtypes() functionprint("AFTER DATATYPE CONVERSION")print(s.convert_dtypes())
Output:
Converting the datatype of a dataframe:
Import module
Create data frame
Check data type
Convert data type using convert_dtypes().dtypes function
The data type of columns are changed accordingly. But the datatype of dataframe will remain object because it contains multiple columns with each column has a different datatype.
Example:
Python3
import pandas as pdimport numpy as np  # creating a dataframedf = pd.DataFrame({"Roll_No.": ([1, 2, 3]),                   "Name": ["Raj", "Ritu", "Rohan"],                   "Result": ["Pass", "Fail", np.nan],                   "Promoted": [True, False, np.nan],                   "Marks": [90.33, 30.6, np.nan]})  # printing the dataframeprint("PRINTING DATAFRAME")display(df)  # checking datatypeprint()print("PRINTING DATATYPE")print(df.dtypes)  # converting datatypeprint()print("AFTER CONVERTING DATATYPE")print(df.convert_dtypes().dtypes)
Output:
Creating the Data frame through series and specifying datatype :
Import module
Create dataframe through series and specify datatype along with it
Check data type
Convert using convert_dtypes().dtypes function
Example:
Python3
import pandas as pdimport numpy as np  # Creating the Data frame through series# and specifying datatype along with itdf = pd.DataFrame({"Column_1": pd.Series([1, 2, 3], dtype=np.dtype("int32")),                   # Column_1 datatype is int32                                        "Column_2": pd.Series(["Apple", "Ball", "Cat"],                                          dtype=np.dtype("object")),                   # Column_2 datatype is 0                                        "Column_3": pd.Series([True, False, np.nan],                                          dtype=np.dtype("object")),                   # Column_3 datatype is 0                                        "Column_4": pd.Series([10, np.nan, 20],                                          dtype=np.dtype("float")),                   # Column_4 datatype is float                                        "Column_5": pd.Series([np.nan, 100.5, 200],                                         dtype=np.dtype("float"))})                   # Column_5 datatype is float  # printing dataframeprint("PRINTING DATAFRAME")display(df)  # checking datatypeprint()print("CHECKING DATATYPE")print(df.dtypes)  # convert datatypeprint()print("AFTER DATATYPE CONVERSION")print(df.convert_dtypes().dtypes)
Output:
Python pandas-datatypes
Python-pandas
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Python | Get unique values from a list
Defaultdict in Python
Python | os.path.join() method
Python Classes and Objects
Create a directory in Python