我有一個數據幀與數千行的兩列像這樣開始: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