我有一个带有字符串列的熊猫数据框。我想要做的是将城市名称与字符串分开。
这是我的 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
如何获得以下输出:
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
慕雪6442864
梵蒂冈之花
相关分类