Check whether only numeric values are present in a column of a DataFrame

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
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Check whether only the numbers are present in a column of a DataFrame using df[‘Numeric’] = list(map(lambda x: x.isdigit(), df[‘Name’])).
  4. 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.
  5. The lambda function will check whether the value in the column contains all the numbers or not.
  6. map() works as an iterator to return a result after applying a function to every item of an iterable 
  7. list() will create a list of all results after applying the lambda function to each value.
  8. 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
				
			

check whether only the lower case is present in a given column of a DataFrame

check whether only alphabetic values present in a column of a DataFrame

Leave a Comment