To convert multiple columns in a DataFrame to specific types, consider using the following methods:
This method can safely convert non-numeric types, such as strings, into integers or floating-point numbers as appropriate. For example:
import pandas as pd table = [ ['a', '1.2', '4.2'], ['b', '70', '0.03'], ['x', '5', '0'], ] df = pd.DataFrame(table) # Convert columns 2 and 3 to floats df[['Column2', 'Column3']] = df[['Column2', 'Column3']].apply(pd.to_numeric)
This method allows explicit conversion to a specified dtype. For example:
df[['Column2', 'Column3']] = df[['Column2', 'Column3']].astype(float)
The choice of method depends on the specific requirements and data structure:
to_numeric(): Ideal for reliable conversion from non-numeric values to numeric types.
astype(): Explicit and flexible conversion to any desired dtype.
infer_objects(): Introduced in pandas 0.21.0, specifically for converting object columns to a more specific type.
convert_dtypes(): Part of pandas version 1.0 and above, automatically converts columns to the "best possible" type that supports pandas' NA missing value.
The above is the detailed content of How Can I Efficiently Change Multiple Column Data Types in Pandas?. For more information, please follow other related articles on the PHP Chinese website!