Home > Back-end >  filter rows from data where column salary has string datatype
filter rows from data where column salary has string datatype

Time:11-13

idk how to ask Question maybe wrong way!

    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

CodePudding user response:

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.

  • Related