YouTip LogoYouTip

Pandas Df Astype

[![Image 1: Pandas Common Functions](#) Pandas Common Functions](#) * * * `df.astype()` is a function in Pandas used to convert the data type of a DataFrame or Series. In the data processing workflow, data type mismatch is a common problem. For example, numbers stored as strings, dates stored as text, etc. `astype()` allows you to explicitly convert data to the desired type, such as integer, float, string, date, etc., ensuring that the data can be correctly calculated and analyzed. * * * ## Basic Syntax and Parameters `astype()` is a member function of both DataFrame and Series, called via the dot operator `.`. ### Syntax Format DataFrame.astype(dtype, copy=True, errors='raise')Series.astype(dtype, copy=True, errors='raise') ### Parameter Description | Parameter | Type | Required | Description | Default Value | | --- | --- | --- | --- | --- | | dtype | Python dtype or numpy dtype | Yes | Target data type. Can be a Python type (e.g., `int`, `str`), NumPy type (e.g., `np.int64`, `np.float32`), or Pandas type (e.g., `'int64'`, `'float64'`, `'category'`). | None | | copy | bool | No | If `True`, always returns a new object; if `False`, modifies the original object in place when possible. | True | | errors | str | No | Controls error handling. `'raise'` means an exception is raised on conversion failure; `'ignore'` means the original data is returned on conversion failure without raising an exception. | 'raise' | ### Return Value Description * Returns a new DataFrame or Series, where the data types of all specified columns have been converted to the target type. * * * ## Examples Let's thoroughly master the usage of `astype()` through a series of examples. ### Example 1: Converting the Data Type of a Single Column Convert a specific column of a Series or DataFrame to a specified type. ## Instance import pandas as pd # Create a DataFrame with values stored as strings data ={ 'Name': ['Zhang San','Li Si','Wang Wu','Zhao Liu'], 'Age': ['25','30','35','28'],# Stored as strings 'Salary': ['5000','6000','5500','7000']# Stored as strings } df = pd.DataFrame(data) print("Original data types:") print(df.dtypes) print("=" * 50) # Convert "Age" column to integer type df['Age']= df['Age'].astype(int) print("Data types after converting Age column:") print(df.dtypes) print("=" * 50) # Convert "Salary" column to float type df['Salary']= df['Salary'].astype(float) print("Data types after converting Salary column:") print(df.dtypes) print("=" * 50) print("Converted data:") print(df) **Expected Output:** Original data types:Name objectAge object # String typeSalary object # String type==================================================Data types after converting Age column:Name objectAge int64 # Integer typeSalary object==================================================Data types after converting Salary column:Name objectAge int64 Salary float64 # Float type==================================================Converted data: Name Age Salary0 Zhang San 25 5000.01 Li Si 30 6000.02 Wang Wu 35 5500.03 Zhao Liu 28 7000.0 **Code Analysis:** 1. In the original data, both age and salary are string types (`object`). 2. Use `df['column_name'].astype(int)` to convert strings to integers. 3. After conversion, numerical calculations such as sum and average can be performed. ### Example 2: Conver
← Pandas Df Sort ValuesPandas Df Replace β†’