How to Check if a Column is Numeric in Pandas or Not : 3 Steps

How to Check if a Column is Numeric in Pandas or Not

Pandas dataframe allows easy and efficient manipulation of dataframe. Let’s say you have dataframe that may contain some numeric column and you want to check if that column is numeric or not. How you can do so? In this tutorial, you will learn how to check if a column is Numeric in pandas or not using simple steps.

Step to Check if a Column is Numeric in Pandas or Not

Let’s know all the steps that will be very helpful in checking whether a column in a dataframe is numeric or not. Just follow each step for deep understanding.

Step 1: Import the library

The first step is to import all the required libraries. In this example, I am using the pandas library that’s why importing it only. Use the import statement to import it.

import pandas as pd

Step 2: Create a sample dataframe

The next step is to create a dummy or sample dataframe that will be very helpful for understanding. However, you can use your own dataframe.

Run the below lines of code to create the data frame.

import pandas as pd
data = {"name":["Rob","Bam","Rob","Rahul"],"age":[23,25,23,32],
"country":["USA","UK","USA","Germany"]}
df = pd.DataFrame(data)
df["age"] = df["age"].astype(int)
df

Output

Sample dataframe to check column is numeric or not
Sample dataframe to check column is numeric or not

Step 3: Define the function to Check if a Column is Numeric in Pandas or Not

In the last step, I will define the function that will check if a Column is Numeric in Pandas or Not. Here I will use the to_numeric() function on a specific column. If the data type of the column values is not numeric then it will raise the error and return False and True if it does not.

Below is the function for the code that returns true or false when the column is numeric or not.

def is_numeric(col):
    try:
        pd.to_numeric(col)
        return True
    except:
        return False

Full Code

import pandas as pd
data = {"name":["Rob","Bam","Rob","Rahul"],"age":[23,25,23,32],
"country":["USA","UK","USA","Germany"]}
df = pd.DataFrame(data)
df["age"] = df["age"].astype(int)
def is_numeric(col):
    try:
        pd.to_numeric(col)
        return True
    except:
        return False
print(is_numeric(df["age"]))
print(is_numeric(df["name"]))

Output

Check if a Column is Numeric or not using to_numeric
Check if a Column is Numeric or not using to_numeric

Conclusion

It’s important for any developer to check the types of all the columns of the dataframe. It allows them to build a good predictive model and take the input for the predictive model of the same type. If you want to check whether the column is numeric or not then you can do so using the above steps.

I hope you have liked this tutorial. If you have any doubt or questions then you can contact us for more information.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner