1
import pandas as pd
data = [['Alex',10],['Bob',12],['Clarke',13] ['Adam', 14]]
df = pd.DataFrame(data,columns=['Name','Age'])
print(df)

    Name    Age
0   Alex    10
1   Bob     12
2   Clarke  13
3   Adam    14

I want to get only Names starting with A . I tried following code mask = df['Name'].str.contains("A*")

 mask
 0    True
 1    True
 2    True
 Name: Name, dtype: bool 

 df = df[mask]

   Name    Age

0    Alex    10
1   Bob      12
2   Clarke   13

but I want to get the result as Name Age

0    Alex    10
TECHI
  • 11
  • 2
  • 5

1 Answers1

4

Use this:

mask = df['Name'].str.startswith("A")

For example:

In [52]: df
Out[52]: 
     Name  Age
0    Alex   10
1     Bob   12
2  Clarke   13
3    Adam   14

In [53]: mask = df['Name'].str.startswith("A")

In [54]: df[mask]
Out[54]: 
   Name  Age
0  Alex   10
3  Adam   14

For regular expression matching, as suggested by @swiftg:

mask = df['Name'].str.match("^A.*")
Ashish Acharya
  • 3,349
  • 1
  • 16
  • 25
  • Also, I want to check for regular expressions like *A*, * , how to handle such cases – TECHI Jul 17 '18 at 18:12
  • 1
    `mask = df['Name'].str.match("^A.*")` for regular expression matching. – swiftg Jul 17 '18 at 18:12
  • @TECHI, * matching is "glob" matching, different from regex. [Please look it up](https://stackoverflow.com/questions/23702202/what-are-the-differences-between-glob-style-pattern-and-regular-expression). – swiftg Jul 17 '18 at 18:13
  • how to handle only * is input then? – TECHI Jul 17 '18 at 18:17