Changing Column Types in Pandas
In pandas, there are several options for converting DataFrame column types. Here's how you can do it:
1. to_numeric()
Use to_numeric() to convert columns with non-numeric values to numeric types (e.g., float or int).
Syntax:
pd.to_numeric(series_or_column, errors='coerce'/'ignore')
Example:
df = pd.DataFrame([[1.2, 4.2], ['70', 0.03]]) df.columns = ['column_1', 'column_2'] # Convert 'column_2' to float df['column_2'] = pd.to_numeric(df['column_2'], errors='coerce')
2. astype()
Use astype() to convert columns to any desired dtype, including numeric and object types.
Syntax:
df.astype(dtype)
Example:
df['column_1'] = df['column_1'].astype(int) df['column_2'] = df['column_2'].astype(float)
3. infer_objects()
Introduced in pandas 0.21.0, infer_objects() converts object columns to more specific dtypes (e.g., integer or float).
Syntax:
df.infer_objects()
Example:
# Create an object DataFrame df = pd.DataFrame({'col1': [1, 2, 3], 'col2': ['a', 'b', 'c']}, dtype='object') # Convert 'col1' to int df['col1'] = df['col1'].infer_objects()
4. convert_dtypes()
Introduced in pandas 1.0, convert_dtypes() converts columns to the "best" dtype that supports missing values.
Syntax:
df.convert_dtypes(infer_objects=True/False)
Example:
# Convert object columns based on inferred types df.convert_dtypes() # Only convert object columns with explicit dtype information df.convert_dtypes(infer_objects=False)
These methods provide flexible options for changing column types in pandas DataFrames. Choose the most appropriate method based on your specific data and requirements.
The above is the detailed content of How Can I Change Column Data Types in Pandas DataFrames?. For more information, please follow other related articles on the PHP Chinese website!