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
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.*")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.