filter rows from data where column salary has string datatype

Question:

    id  name    salary
0   1   shyam   10000
1   2   ram     20000
2   3   ravi    abc
3   4   abhay   30000
4   5   karan   fgh

expected:

       id   name    salary
  2    3    ravi    abc
  4    5    karan   fgh
Asked By: Rupesh chauhan

||

Answers:

We can use str.contains as follows:

df_out = df[(df["name"].str.contains(r'^[A-Za-z]+$', regex=True)) &
            (df["salary"].str.contains(r'^[A-Za-z]+$', regex=True))]

The above logic will only match rows for which both the name and salary columns contain only alpha characters.

Answered By: Tim Biegeleisen