Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PANDAS Python | Contain specific value in specific position

I’m trying to select just the rows that on the column "Cuenta" contain "05" in the third and fourth position , for example : 51050300 , 51050600

Año Periodo Cuenta
2023 1 51050300
2023 2 51053900
2023 1 74359570
2023 2 74452500
2023 6 51050300
2023 7 51050600
2023 7 52351005
2023 7 52353505
2023 7 52159500

I’m using this code:

pattern=r'..05*' 

df[df['Cuenta'].str.contains(pattern)]

But it doesn´t work, How can I do it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have to change your pattern:

pattern = '^..05'  # ^ means from the begin string

>>> df['Cuenta'].astype(str).str.contains(pattern)
0     True
1     True
2    False
3    False
4     True
5     True
6    False
7    False
8    False
Name: Cuenta, dtype: bool
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading