Home > OS >  Pandas: Remove all characters before a specific character in a dataframe column
Pandas: Remove all characters before a specific character in a dataframe column

Time:11-27

how can I remove all characters before a specific character in a Dataframe column? In this example remove everything BEFORE the first comma (,) and of course the companies names will always be of varying length and rarely the same but always before the first comma.

My Dataframe:

    address
0   My Company Ltd, address, city, state, postcode, country
1   Business Plc, address, city, state, postcode, country
2   Work Harder Inc, address, city, state, postcode, country
3   Company Business People, address, city, state, postcode, country

Desired outcome:

    address
0   address, city, state, postcode, country
1   address, city, state, postcode, country
2   address, city, state, postcode, country
3   address, city, state, postcode, country

CodePudding user response:

Using str.replace:

df["address"] = df["address"].str.replace(r'^[^,]*,\s*', '')

Here is a regex demo showing that the logic is working.

  • Related