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?
>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