In this python pandas program, we will only alphabetic values 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 alphabets are present in a record of a column of a DataFrame using df[‘Characters’] = list(map(lambda x: x.isalpha(), df[‘Name’])).
- It will create a new column in the dataframe that will contain True if the value in the row contains only the alphabet or else False.
- The lambda function will check whether the value in the column contains only the alphabet 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['Characters'] = list(map(lambda x: x.isalpha(), df['Marks']))
print(df)
Output :
0 Marks Characters
0 Pass True
1 88 False
2 First Class False
3 90 False
4 Distinction True