Alphabetic-only check

Python Practice › Pandas Programs › Program 74

Alphabetic-only check

EasyPandas Programs · 74 of 95

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

Input
["Pass", "88", "First class", "90", "Distinction"]
Output
         Marks  is_alpha
0         Pass      True
1           88     False
2  First class     False
3           90     False
4  Distinction      True

Approach

  1. str.isalpha(); spaces make it False.

Solution

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

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Numeric-only check

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.

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

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Lowercase-only check

Python Practice › Pandas Programs › Program 72

Lowercase-only check

EasyPandas Programs · 72 of 95

Write a pandas program to check which values are fully lowercase.

Input
["kate", "jason", "ROBERT", "MARK", "dwyane"]
Output
     Name  is_lower
0    kate      True
1   jason      True
2  ROBERT     False
3    MARK     False
4  dwyane      True

Approach

  1. str.islower().

Solution

Python
import pandas as pddf = pd.DataFrame({"Name": ["kate", "jason", "ROBERT", "MARK", "dwyane"]})df["is_lower"] = df["Name"].str.islower()print(df)
Output
     Name  is_lower
0    kate      True
1   jason      True
2  ROBERT     False
3    MARK     False
4  dwyane      True

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Uppercase-only check

Python Practice › Pandas Programs › Program 71

Uppercase-only check

EasyPandas Programs · 71 of 95

Write a pandas program to check which values are fully uppercase.

Input
["Kate", "Jason", "ROBERT", "MARK", "Dwyane"]
Output
     Name  is_upper
0    Kate     False
1   Jason     False
2  ROBERT      True
3    MARK      True
4  Dwyane     False

Approach

  1. str.isupper().

Solution

Python
import pandas as pddf = pd.DataFrame({"Name": ["Kate", "Jason", "ROBERT", "MARK", "Dwyane"]})df["is_upper"] = df["Name"].str.isupper()print(df)
Output
     Name  is_upper
0    Kate     False
1   Jason     False
2  ROBERT      True
3    MARK      True
4  Dwyane     False

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Capitalise text values

Python Practice › Pandas Programs › Program 70

Capitalise text values

EasyPandas Programs · 70 of 95

Write a pandas program to capitalise the text values of a column.

Input
["cricket", "Yes", "pass", "FAIL"]
Output
0    Cricket
1        Yes
2       Pass
3       Fail
dtype: str

Approach

  1. str.capitalize().

Solution

Python
import pandas as pds = pd.Series(["cricket", "Yes", "pass", "FAIL"])print(s.str.capitalize())
Output
0    Cricket
1        Yes
2       Pass
3       Fail
dtype: str

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Select text columns

Python Practice › Pandas Programs › Program 69

Select text columns

EasyPandas Programs · 69 of 95

Write a pandas program to select the text columns.

Output
   name   city
0  Alex   Pune
1  John  Delhi

Approach

  1. select_dtypes(include=...).

Solution

Python
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John"], "city": ["Pune", "Delhi"], "age": [30, 27]})print(df.select_dtypes(exclude="number"))
Output
   name   city
0  Alex   Pune
1  John  Delhi

Line by line

Line 3

Excluding numeric columns works on both old (object) and new (str) text types.

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Reverse column order

Python Practice › Pandas Programs › Program 68

Reverse column order

EasyPandas Programs · 68 of 95

Write a pandas program to reverse the order of columns.

Output
   salary  age  name
0   50000   30  Alex
1   65000   27  John

Approach

  1. df[df.columns[::-1]].

Solution

Python
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John"], "age": [30, 27], "salary": [50000, 65000]})print(df[df.columns[::-1]])
Output
   salary  age  name
0   50000   30  Alex
1   65000   27  John

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Reverse row order

Python Practice › Pandas Programs › Program 67

Reverse row order

EasyPandas Programs · 67 of 95

Write a pandas program to reverse the order of rows.

Output
    name  age
2  Peter   29
1   John   27
0   Alex   30

Approach

  1. iloc[::-1].

Solution

Python
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter"], "age": [30, 27, 29]})print(df.iloc[::-1])
Output
    name  age
2  Peter   29
1   John   27
0   Alex   30

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Remove the last n rows

Python Practice › Pandas Programs › Program 66

Remove the last n rows

EasyPandas Programs · 66 of 95

Write a pandas program to remove the last n rows.

Output
   name  age
0  Alex   30
1  John   27

Approach

  1. iloc[:-n].

Solution

Python
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter", "Klaus"], "age": [30, 27, 29, 33]})print(df.iloc[:-2])
Output
   name  age
0  Alex   30
1  John   27

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Remove the first n rows

Python Practice › Pandas Programs › Program 65

Remove the first n rows

EasyPandas Programs · 65 of 95

Write a pandas program to remove the first n rows.

Output
    name  age
2  Peter   29
3  Klaus   33

Approach

  1. iloc[n:].

Solution

Python
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter", "Klaus"], "age": [30, 27, 29, 33]})print(df.iloc[2:])
Output
    name  age
2  Peter   29
3  Klaus   33

Learn the concept

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

More pandas programs

← All 95 Pandas Programs