简体   繁体   中英

How to remove records with first three numeric caracters? pandas/python

I have some code that groups by the first 3 characters of a selected column:

res = (excel_file.groupby([excel_file["Column"].str[:3]])
                       .sum()
                       .reset_index()
                       .replace(replace_map))

I need it to also exclude values ​​where the first three characters are numbers. And did not include new excluded rows in the res table. Please help with implementation.

Just match and exclude the rows starting with three digits:

idx = excel_file["Column"].str.match('^\d{3}')

res = (excel_file.groupby([excel_file["Column"][~idx].str[:3]])
                       .sum()
                       .reset_index()
                       .replace(replace_map))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM