Pandas DataFrame infer_objects() Method
Example
Convert the data types to better fit the content:
import pandas as pd
data = {
"age": ["fifty", 40, 30],
"qualified": ["No", True, False]
}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
print("Original dtypes:")
print(df.dtypes)
#Remove the first row in both colums:
df =
df.iloc[1:]
print("New DataFrame:")
print(df)
newdf =
df.infer_objects()
print("New dtypes:")
print(newdf.dtypes)
Try it Yourself »
Definition and Usage
The infer_objects()
method returns a new DataFrame
where each column has been changed to the best possible data type.
Syntax
dataframe.infer_objects()
Parameters
The infer_objects()
method takes no paramters.
Return Value
a Pandas DataFrame with the converted result.