【发布时间】:2018-02-12 08:06:28
【问题描述】:
我有一个包含数千行和两列的 DataFrame,如下所示:
string state
0 the best new york cheesecake rochester ny ny
1 the best dallas bbq houston tx random str tx
2 la jolla fish shop of san diego san diego ca ca
3 nothing here dc
对于每个州,我都有一个所有城市名称(小写)的正则表达式,其结构类似于(city1|city2|city3|...),其中城市的顺序是任意的(但如果需要可以更改)。例如,纽约州的正则表达式包含 'new york' 和 'rochester'(同样,德克萨斯州的 'dallas' 和 'houston',加利福尼亚州的 'san diego' 和 'la jolla')。
我想找出字符串中最后出现的城市是什么(对于观察 1、2、3、4,我想要 'rochester'、'houston'、'san diego' 和 NaN(或无论如何),分别)。
我从str.extract 开始,并试图考虑诸如反转字符串之类的事情,但已经陷入僵局。
非常感谢您的帮助!
【问题讨论】:
标签: python regex string pandas series