bbennett36
bbennett36

Reputation: 6355

Python - Removing strings that follow a wildcard pattern

Initial Data (String datatype)

Los Gatos 50K
Las Palmas Canary Islands 25K
Roland Garros
Seoul 25K
Rome

Desired Result

Los Gatos
Las Palmas Canary Islands
Roland Garros
Seoul
Rome

I am looking for a way to remove any string pattern that is 2 digits and then a K. But it needs to be able to handle any 2 values before the K. I haven't seen any answers that use a wildcard for the part of the replace. It should be something like this (I know this is not valid) -
data.replace("**K", '')

Side note - This string will be a column in a dataframe so if there is an easy solution that works with that would be ideal. If not I can iterate through each row and transform it that way.

Upvotes: 2

Views: 2268

Answers (1)

Vaishali
Vaishali

Reputation: 38415

Try

df = df.replace('\d{2}K', '', regex = True)


    0
0   Los Gatos
1   Las Palmas Canary Islands
2   Roland Garros
3   Seoul
4   Rome

Upvotes: 5

Related Questions