Reputation: 5299
I have dataframe(series) like below
12a 3
4 b5
6 b7c
9 c1d
I would like to extract rows which contains letter 'b'(start position=3,in text)
4 b5
6 b7c
How can I extract these rows in pandas dataframe?
Upvotes: 0
Views: 50
Reputation: 862791
You can use indexing with str
and str.contains
with boolean indexing
:
df = pd.DataFrame({'a':['12a 3','4 b5','6 b7c','9 c1d']})
print (df)
a
0 12a 3
1 4 b5
2 6 b7c
3 9 c1d
df1 = df[df['a'].str[2:].str.contains('b')]
print (df1)
a
1 4 b5
2 6 b7c
If need check only 3rd letter:
df1 = df[df['a'].str[2] == 'b']
print (df1)
a
1 4 b5
2 6 b7c
For multiple values is possible use:
df1 = df[df['a'].str[2:].str.contains('[ab]')]
print (df1)
a
0 12a 3
1 4 b5
2 6 b7c
And for check 3rd leter:
df1 = df[df['a'].str[2].isin(['a','b'])]
print (df1)
a
0 12a 3
1 4 b5
2 6 b7c
df1 = df[df['a'].str[2].isin(list('ab'))]
print (df1)
a
0 12a 3
1 4 b5
2 6 b7c
Upvotes: 1