如何在字典和数据框之间进行模糊字符串匹配

时间:2019-07-08 16:54:06

标签: python pandas dataframe dictionary fuzzywuzzy

我有这样的字典

Station                    Latitude        Longitude
London Bridge            51.50467435      -0.086005598
Kings Cross              51.53079819      -0.122756421
Kings Cross St. Pancras  51.53031246      -0.12387213
Euston Square            51.52866433      -0.133296472
Leicester Square         51.51129118      -0.128242148
Russell Square           51.52301304      -0.124590198
Sloane Square            51.49235976      -0.156488857
...

我想将df中的['ORIGIN']和['DESTINATION']列与字典匹配,并获取它们的坐标。

df:

  ORIGIN                DESTINATION              origin_coor     destination_coor  
  St. Pancras           london bridge/farringdon
  London' Kings Cross      Euston Sq
  ...

理想的结果是:

  ORIGIN                    DESTINATION            origin_coor     destination_coor  
 St. Pancras      london bridge/farringdon    (51.53031246,-0.12387213) (51.50467435,-0.086005598)
 London's Kings Cross      Euston Sq       (51.53079819,-0.122756421) (51.52866433,-0.133296472)
          ...

我的代码如下:

import pandas as pd
df = pd.DataFrame({'ORIGIN': ['Kings Cross St. Pancras', 'Kings Cross'],
                   'DESTINATION': ['London Bridge', 'Euston Square']})

geo = {'London Bridge':(51.50467435, -0.086005598),
       'Euston Square':(51.53031246, -0.12387213),
       'Kings Cross':(51.49235976, -0.124590198),
       'Kings Cross St. Pancras':(51.52301304, -0.086005598)
      }

df['origin_coor'] = df['ORIGIN'].map(geo)
df['destination_coor'] = df['DESTINATION'].map(geo)

df

但是问题是我的字典中的地名与数据框之间存在差异,因此我不能简单地使用地图功能。有人有解决方案吗?

0 个答案:

没有答案