Python Practice › Pandas Programs › Program 73

Numeric-only check

EasyPandas Programs · 73 of 95

Write a pandas program to check which values contain only digits.

Advertisement
Input
["Pass", "88", "2nd class", "90", "Distinction"]
Output
         Marks  is_numeric
0         Pass       False
1           88        True
2    2nd class       False
3           90        True
4  Distinction       False

Approach

  1. str.isnumeric().

Solution

Python
import pandas as pddf = pd.DataFrame({"Marks": ["Pass", "88", "2nd class", "90", "Distinction"]})df["is_numeric"] = df["Marks"].str.isnumeric()print(df)
Output
         Marks  is_numeric
0         Pass       False
1           88        True
2    2nd class       False
3           90        True
4  Distinction       False
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs