How to check if float pandas column contains only integer numbers? How to check if float pandas column contains only integer numbers? python python

How to check if float pandas column contains only integer numbers?


Comparison with astype(int)

Tentatively convert your column to int and test with np.array_equal:

np.array_equal(df.v, df.v.astype(int))True

float.is_integer

You can use this python function in conjunction with an apply:

df.v.apply(float.is_integer).all()True

Or, using python's all in a generator comprehension, for space efficiency:

all(x.is_integer() for x in df.v)True


Here's a simpler, and probably faster, approach:

(df[col] % 1  == 0).all()

To ignore nulls:

(df[col].fillna(-9999) % 1  == 0).all()


If you want to check multiple float columns in your dataframe, you can do the following:

col_should_be_int = df.select_dtypes(include=['float']).applymap(float.is_integer).all()float_to_int_cols = col_should_be_int[col_should_be_int].indexdf.loc[:, float_to_int_cols] = df.loc[:, float_to_int_cols].astype(int)

Keep in mind that a float column, containing all integers will not get selected if it has np.NaN values. To cast float columns with missing values to integer, you need to fill/remove missing values, for example, with median imputation:

float_cols = df.select_dtypes(include=['float'])float_cols = float_cols.fillna(float_cols.median().round()) # median imputationcol_should_be_int = float_cols.applymap(float.is_integer).all()float_to_int_cols = col_should_be_int[col_should_be_int].indexdf.loc[:, float_to_int_cols] = float_cols[float_to_int_cols].astype(int)