I have the following Pandas Dataframe:
d = {'col1': ["aaa", "bbb", "ccc", "ddd", "acc", "bcc"]}
df = pd.DataFrame(data=d)
df
Output of the above code:
col1
0 aaa
1 bbb
2 ccc
3 ddd
4 abb
5 bcc
I need to get the rows where the column value starts with – say – either "a" or "c". After the filtering, the result should look as the following:
col1
0 aaa
1 ccc
2 abb
How can I achieve this without using a for loop?
>Solution :
Just for a and c
df = df[df["col1"].str.match('^[ac].+')]
You can include as many letters in the square brackets that you need to match on.
df = df[df["col1"].str.match('^[abcdefg].+')]