Python Forum

Full Version: Filter numbers (that start with 1)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi how filter number in column CFOP that which start with number 1xxx

I have a column CFOP with number type like this (Dtype int64)
CFOP
5927
5403
5102
6152
6209
1102
2353
1353
1556
2923
2102
1411
1202
2152
1949
6202

But I need get number that start 1

import pandas as pd

cols = ['CFOP', 'QUANTIDADE', 'VC', 'BC']
xl = pd.read_excel(r'C:\Users\alexandre.goncalves\Documents\Projetos_Python\Fat_3.xlsx',
                   sheet_name='geral', usecols=cols)
# How to filter data in column CFOP which start with number 1, I try this
print(xl.loc[xl['CFOP'].isin([1102,1353,1556,1411,1202,1949])])
I would convert the column to the string type and use startswith, e.g.

xl.loc[xl['CFOP'].astype(str).str.startswith('1')]
Hi scidam

Good ideia, thank you!