Home »
Python »
Python Programs
Finding non-numeric rows in dataframe in pandas
Given a Pandas DataFrame, we have to find non-numeric rows.
Submitted by Pranit Sharma, on July 23, 2022
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
Sometimes, while dealing with a large data set, we deal with every kind of data type but we need some specific data types.
For instance, we sometimes need to find non-numeric rows in DataFrame, pandas allow us to achieve this task. For this purpose, we will first use the map() method which will help us to traverse each value of DataFrame so that we can check the value at the same time. We will check the value using np.isreal() method.
np.isreal() method returns Boolean value. If it returns True, it means that the value is numeric and if False is the result, then the value is non-numeric.
In this way, we can check that a row is numeric or non-numeric values in it.
Let us understand with the help of an example,
Python code to find non-numeric rows in dataframe in pandas
# Importing pandas package
import pandas as pd
# Importing numpy package
import numpy as np
# Creating a Dictionary
d = {
'col1':[12,24,36,48,60],
'col2':[13,26,39,52,65],
'col3':['a','b','c','d','e']
}
# Creating a DataFrame
df = pd.DataFrame(d)
# Display original DataFrame
print("Original DataFrame:\n",df,"\n")
# Checking values
result = df.applymap(np.isreal)
# Display result
print("Result:\n",result)
Output:
Python Pandas Programs »