In this python pandas program, we will check whether only numeric values are present in a column of a DataFrame using the pandas library.
Steps to solve the program
- Import pandas library as pd.
- Create a dataframe using pd.DataFrame().
- Check whether only the numbers are present in a column of a DataFrame using df[‘Numeric’] = list(map(lambda x: x.isdigit(), df[‘Name’])).
- It will create a new column in the dataframe that will contain True if the value in the row contains all the numbers characters or else False.
- The lambda function will check whether the value in the column contains all the numbers or not.
- map() works as an iterator to return a result after applying a function to every item of an iterable
- list() will create a list of all results after applying the lambda function to each value.
- Print the output.
import pandas as pd
d = {'Marks':['Pass','88','First Class','90','Distinction']}
df = pd.DataFrame(d)
df['Numeric'] = list(map(lambda x: x.isdigit(), df['Marks']))
print(df)
Output :
0 Marks Numeric
0 Pass False
1 88 True
2 First Class False
3 90 True
4 Distinction False