📜  astype oandas (1)

📅  最后修改于: 2023-12-03 14:59:24.815000             🧑  作者: Mango

Introduction to astype in pandas

The astype function in pandas is used to change the data type of a column in a DataFrame or a Series. It enables you to convert a column to a different data type, such as changing a numeric column to a categorical column or vice versa, or converting a string column to a datetime column.

Syntax

The syntax for using astype is as follows:

DataFrame.astype(dtype, copy=True, errors='raise')
Series.astype(dtype, copy=True, errors='raise')
  • dtype: The data type to which the column should be converted.
  • copy (optional): Specifies whether to return a new copy of the data or make the changes in place. By default, it is set to True.
  • errors (optional): Specifies how to handle errors during the conversion. It can take three values - 'raise' (default), 'ignore', and 'coerce'.
Examples
Change Data Type of a Single Column in a DataFrame
import pandas as pd

# Create a DataFrame
data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
df = pd.DataFrame(data)

# Check the initial data types of the columns
print(df.dtypes)
# Output:
# A    int64
# B    int64
# C    int64
# dtype: object

# Change the data type of column 'A' to float
df['A'] = df['A'].astype(float)

# Check the updated data types of the columns
print(df.dtypes)
# Output:
# A    float64
# B      int64
# C      int64
# dtype: object
Convert a Numeric Column to Categorical
import pandas as pd

# Create a DataFrame
data = {'Grade': [89, 92, 78, 95, 85]}
df = pd.DataFrame(data)

# Convert the 'Grade' column to categorical
df['Grade'] = df['Grade'].astype('category')

# Check the updated data types of the columns
print(df.dtypes)
# Output:
# Grade    category
# dtype: object
Convert a String Column to Datetime
import pandas as pd

# Create a DataFrame
data = {'Date': ['2022-06-01', '2022-06-02', '2022-06-03']}
df = pd.DataFrame(data)

# Convert the 'Date' column to datetime
df['Date'] = pd.to_datetime(df['Date'])

# Check the updated data types of the columns
print(df.dtypes)
# Output:
# Date    datetime64[ns]
# dtype: object
Conclusion

The astype function in pandas is a convenient way to convert the data type of a column in a DataFrame or a Series. It provides flexibility in converting columns to different data types based on your requirements.