Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python.pandas: how to select rows where objects start with letters 'PL'

I have specific problem with pandas: I need to select rows in dataframe which start with specific letters. Details: I've imported my data to dataframe and selected columns that I need. I've also narrowed it down to row index I need. Now I also need to select rows in other column where objects START with letters 'pl'.

Pasted here picture of how df looks like now for better view

Is there any solution to select row only based on first two characters in it?

I was thinking about

pl = df[‘Code’] == pl*

but it won't work due to row indexing. Advise appreciated!

like image 519
isdedal Avatar asked Feb 03 '26 05:02

isdedal


1 Answers

Use startswith for this:

df = df[df['Code'].str.startswith('pl')]
like image 120
NYC Coder Avatar answered Feb 04 '26 18:02

NYC Coder



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!