Home > Article > Backend Development > How to Convert a Pandas Column with NaN Values to Integer Type?
Converting Pandas Column Containing NaN to Dtype Int
To convert a Pandas column containing missing values (NaNs) to integer type, pandas version 0.24. introduces the nullable Integer Data Type, represented by IntegerArray.
Nullable Integer Data Type
Arrays.IntegerArray allows the representation of integer data with missing values. It differs from the default integer dtype and must be explicitly specified when creating an array or Series.
Example:
import pandas as pd arr = pd.array([1, 2, np.nan], dtype=pd.Int64Dtype()) pd.Series(arr) # Output: 0 1 1 2 2 NaN dtype: Int64
Converting a Column to Nullable Integers
df['myCol'] = df['myCol'].astype('Int64')
This will convert the column 'myCol' to nullable integers, allowing missing values to be represented as NaN.
The above is the detailed content of How to Convert a Pandas Column with NaN Values to Integer Type?. For more information, please follow other related articles on the PHP Chinese website!