close

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

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to check if float pandas column contains only integer numbers in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

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

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

    Tentatively convert your column to int and test with np.array_equal:
    np.array_equal(df.v, df.v.astype(int)) True

  2. check if float pandas column contains only integer numbers

    Tentatively convert your column to int and test with np.array_equal:
    np.array_equal(df.v, df.v.astype(int)) True

Method 1

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

Method 2

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

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

To ignore nulls:

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

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read