Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: select dataframe rows only if the values in a specific column start with

I have the following dataframe df1:

    X           Y           A       B
0   484         408         10      3360
1   478         415         24      3365
2   504         452         31      yes
3   613         551         33      maybe
4   663         665         39      no

I know how to select the row for which column B is yes or any other specific value:

df1.loc[df1['B'] == 'yes']

But how can I select all the rows that do not start with 336?

PS: in my case, 3360 and 3365 are strings.

like image 850
FaCoffee Avatar asked Jan 01 '26 00:01

FaCoffee


1 Answers

I would use something like df[~df.B.str.startswith('336')], using the str accessor. For instance,

>>> df = pd.DataFrame({'B': ['3360', '3365', 'yes', 'maybe', 'no']})
>>> df[~df.B.str.startswith('336')]
       B
2    yes
3  maybe
4     no

And if you have multiple strings to check, startswith accepts a tuple of prefixes.

>>> df[~df.B.str.startswith(('112', '336', 'n'))]
       B
2    yes
3  maybe
like image 85
miradulo Avatar answered Jan 02 '26 14:01

miradulo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!