I have a pandas dataframe with string column. What I am trying to do is separate the city name from the string.
Here is my MWE:
import numpy as np
import pandas as pd
data = """\
2930 Beverly Glen Circle Los Angeles
435 S. La Cienega Blvd. Los Angeles
12224 Ventura Blvd. Studio City
9570 Wilshire Blvd. Beverly Hills
26025 Pacific Coast Hwy. Malibu""".split('\n')
df = pd.DataFrame(data)
print(df)
cities = ['Los Angeles', 'Studio City', 'Beverly Hills','Malibu']
pat = '|'.join([r'(.*)\s({city})' for city in cities])
df = df[0].str.extract(pat,expand=True)
df
How to get the following output:
0 addr city
0 2930 Beverly Glen Circle Los Angeles 2930 Beverly Glen Circle Los Angeles
1 435 S. La Cienega Blvd. Los Angeles 435 S. La Cienega Blvd. Los Angeles
2 12224 Ventura Blvd. Studio City 12224 Ventura Blvd. Studio City
3 9570 Wilshire Blvd. Beverly Hills 9570 Wilshire Blvd. Beverly Hills
4 26025 Pacific Coast Hwy. Malibu 26025 Pacific Coast Hwy. Malibu