我有一个数据帧与数千行的两列像这样开始:str.extract从熊猫数据帧后
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
开始,并试图想像颠倒弦线但陷入僵局。
非常感谢您的帮助!
第一个解决方案已经足够好了;谢谢! – user49007
@ user49007 - 感谢您的纠正。 – jezrael