Pandas2.2 DataFrame
Conversion
method | describe |
---|---|
(dtype[, copy, errors]) | Used to convert data in DataFrame to a specified data type |
DataFrame.convert_dtypes([infer_objects, …]) | Used to convert data types in DataFrame to more appropriate types |
.convert_dtypes
.convert_dtypes
is a method to convert data types in DataFrame to more appropriate types. This method can help automatically infer and convert data types, making data processing more efficient and accurate.
Method signature
DataFrame.convert_dtypes(infer_objects=True, convert_string=True, convert_integer=True, convert_boolean=True, convert_floating=True, dtype_backend='numpy_nullable')
Parameter description
-
infer_objects
: Boolean value, default isTrue
, indicating whether to try toobject
Convert a column of type to a more specific type (e.g.int64
orfloat64
)。 -
convert_string
: Boolean value, default isTrue
, indicating whether it willobject
Convert columns of type tostring
type. -
convert_integer
: Boolean value, default isTrue
, indicating whether it willobject
Convert columns of type tointeger
type. -
convert_boolean
: Boolean value, default isTrue
, indicating whether it willobject
Convert columns of type toboolean
type. -
convert_floating
: Boolean value, default isTrue
, indicating whether it willobject
Convert columns of type tofloating
type. -
dtype_backend
: string, default is'numpy_nullable'
, indicates the data type backend used. Could be'numpy_nullable'
or'pyarrow'
。
Return value
- Returns a new DataFrame where the data type has been converted.
Example
Suppose there is a DataFrame as follows:
import pandas as pd data = { 'A': ['1', '2', '3'], 'B': ['1.1', '2.2', '3.3'], 'C': ['True', 'False', 'True'], 'D': ['x', 'y', 'z'] } df = (data) print("Original DataFrame:") print(df) print("\nData Type:") print()
Output:
Original DataFrame:
A B C D
0 1 1.1 True x
1 2 2.2 False y
2 3 3.3 True zData Type:
A object
B object
C object
D object
dtype: object
Example 1: Convert data types using default parameters
df_converted = df.convert_dtypes() print("Converted DataFrame:") print(df_converted) print("\nData Type:") print(df_converted.dtypes)
result:
The converted DataFrame:
A B C D
0 1 1.1 True x
1 2 2.2 False y
2 3 3.3 True zData Type:
A Int64
B Float64
C boolean
D string
dtype: object
Example 2: Disable convert_string
df_converted_no_string = df.convert_dtypes(convert_string=False) print("DataFrame after convert_string is disabled:") print(df_converted_no_string) print("\nData Type:") print(df_converted_no_string.dtypes)
result:
Disable DataFrame after convert_string:
A B C D
0 1 1.1 True x
1 2 2.2 False y
2 3 3.3 True zData Type:
A Int64
B Float64
C boolean
D object
dtype: object
Example 3: Disable convert_integer
df_converted_no_integer = df.convert_dtypes(convert_integer=False) print("DataFrame after convert_integer is disabled:") print(df_converted_no_integer) print("\nData Type:") print(df_converted_no_integer.dtypes)
result:
Disable DataFrame after convert_integer:
A B C D
0 1 1.1 True x
1 2 2.2 False y
2 3 3.3 True zData Type:
A object
B Float64
C boolean
D string
dtype: object
Example 4: Disable convert_boolean
df_converted_no_boolean = df.convert_dtypes(convert_boolean=False) print("DataFrame after convert_boolean is disabled:") print(df_converted_no_boolean) print("\nData Type:") print(df_converted_no_boolean.dtypes)
result:
Disable DataFrame after convert_boolean:
A B C D
0 1 1.1 True x
1 2 2.2 False y
2 3 3.3 True zData Type:
A Int64
B Float64
C object
D string
dtype: object
Example 5: Disable convert_floating
df_converted_no_floating = df.convert_dtypes(convert_floating=False) print("Disable DataFrame after convert_floating:") print(df_converted_no_floating) print("\nData Type:") print(df_converted_no_floating.dtypes)
result:
Disable DataFrame after convert_floating:
A B C D
0 1 1.1 True x
1 2 2.2 False y
2 3 3.3 True zData Type:
A Int64
B object
C boolean
D string
dtype: object
Through these examples, you can see.convert_dtypes
How to automatically infer and convert data types in DataFrame. These methods are very useful when preprocessing data and type conversion.
Things to note
-
convert_dtypes
Methods can convert data types in DataFrame to more appropriate types. - You can control which types of conversions should be enabled or disabled by setting different parameters.
-
dtype_backend
Parameters can specify the data type backend used, for example'numpy_nullable'
or'pyarrow'
。 - These transformations can help improve the efficiency and accuracy of data processing.
Sample code and verification
For verification.convert_dtypes
The effect of the method, you can run the above sample code and view the output results.
import pandas as pd # Create a sample DataFramedata = { 'A': ['1', '2', '3'], 'B': ['1.1', '2.2', '3.3'], 'C': ['True', 'False', 'True'], 'D': ['x', 'y', 'z'] } df = (data) print("Original DataFrame:") print(df) print("\nData Type:") print() # Use default parameters to convert data typesdf_converted = df.convert_dtypes() print("\nConverted DataFrame:") print(df_converted) print("\nData Type:") print(df_converted.dtypes) # Disable convert_stringdf_converted_no_string = df.convert_dtypes(convert_string=False) print("\nDisable DataFrame after convert_string:") print(df_converted_no_string) print("\nData Type:") print(df_converted_no_string.dtypes) # Disable convert_integerdf_converted_no_integer = df.convert_dtypes(convert_integer=False) print("\nDisable DataFrame after convert_integer:") print(df_converted_no_integer) print("\nData Type:") print(df_converted_no_integer.dtypes) # Disable convert_booleandf_converted_no_boolean = df.convert_dtypes(convert_boolean=False) print("\nDisable DataFrame after convert_boolean:") print(df_converted_no_boolean) print("\nData Type:") print(df_converted_no_boolean.dtypes) # Disable convert_floatingdf_converted_no_floating = df.convert_dtypes(convert_floating=False) print("\nDisable DataFrame after convert_floating:") print(df_converted_no_floating) print("\nData Type:") print(df_converted_no_floating.dtypes)
Running results
After running the above code, you will see the following output:
Original DataFrame:
A B C D
0 1 1.1 True x
1 2 2.2 False y
2 3 3.3 True zData Type:
A object
B object
C object
D object
dtype: objectThe converted DataFrame:
A B C D
0 1 1.1 True x
1 2 2.2 False y
2 3 3.3 True zData Type:
A Int64
B Float64
C boolean
D string
dtype: objectDisable DataFrame after convert_string:
A B C D
0 1 1.1 True x
1 2 2.2 False y
2 3 3.3 True zData Type:
A Int64
B Float64
C boolean
D object
dtype: objectDisable DataFrame after convert_integer:
A B C D
0 1 1.1 True x
1 2 2.2 False y
2 3 3.3 True zData Type:
A object
B Float64
C boolean
D string
dtype: objectDisable DataFrame after convert_boolean:
A B C D
0 1 1.1 True x
1 2 2.2 False y
2 3 3.3 True zData Type:
A Int64
B Float64
C object
D string
dtype: objectDisable DataFrame after convert_floating:
A B C D
0 1 1.1 True x
1 2 2.2 False y
2 3 3.3 True zData Type:
A Int64
B object
C boolean
D string
dtype: object
Through these examples, you can see.convert_dtypes
How to automatically infer and convert data types in DataFrame. These methods are very useful when preprocessing data and type conversion.
This is the article about the specific use of pandas DataFrame convert_dtypes. For more related pandas DataFrame convert_dtypes, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!