【问题标题】:Update a null values in countrycode column in a data frame by matching substring of country name using python通过使用python匹配国家名称的子字符串来更新数据框中国家代码列中的空值
【发布时间】:2016-09-26 15:02:17
【问题描述】:

我有两个数据框:灾难,CountryInfo 灾难有一个国家代码列,其中有一些空值,例如:

灾难:

 1.**Country**              - **Country_code** 
 2.India                    - Null         
 3.Afghanistan (the)        - AFD
 4.India                    - IND
 5.United States of America - Null

国家信息:

0.**CountryName**   - **ISO** 
1.India             - IND
2.Afganistan        - AFD
3.United States     - US

预期结果

          Country Country_code
 0          India          IND
 1    Afghanistan          AFD
 2          India          IND
 3  United States           US

我需要参考国家名称的子字符串填写国家代码。有人可以为此提出解决方案吗?

【问题讨论】:

    标签: python python-2.7 pandas


    【解决方案1】:

    应该这样做。您需要使用rename 更改列名,以便dataframes 具有相同的列名。然后,difflib 模块及其get_close_matches 方法可用于对Country 名称进行模糊匹配和替换。那么合并dataframes就很简单了

    import pandas as pd
    import numpy as np
    import difflib
    
    df1 = pd.DataFrame({'Country' : ['India', 'Afghanistan', 'India', 'United States of America'],
                            'Country_code' : ['Null', 'AFD', 'IND', 'Null']})
    df1
                        Country Country_code
    0                     India         Null
    1               Afghanistan          AFD
    2                     India          IND
    3  United States of America         Null
    
    df2 = pd.DataFrame({'Country' : ['India', 'Afghanistan', 'India', 'United States'],
                        'ISO' : ['IND', 'AFD', 'IND', 'USA']})
    df2
              Country ISO
    0          India  IND
    1    Afghanistan  AFD
    2          India  IND
    3  United States  USA
    
    df2.rename(columns={'ISO' : 'Country_code'}, inplace=True)
    df2
             Country Country_code
    0          India          IND
    1    Afghanistan          AFD
    2          India          IND
    3  United States          USA
    

    以下代码会将df2 中的Country 列更改为df1Country 列中提供最接近匹配的名称。这是一种对子字符串执行“模糊连接”的方法。

    df1['Country'] = df1.Country.map(lambda x: difflib.get_close_matches(x, df2.Country)[0])
    df1
             Country Country_code
    0          India         Null
    1    Afghanistan          AFD
    2          India          IND
    3  United States         Null
    

    现在您可以简单地 mergedataframes,这将更新 df1 中缺少的 Country_code 行。

    df1.merge(df2, how='right', on=['Country', 'Country_code'])
    
             Country Country_code
    0    Afghanistan          AFD
    1          India          IND
    2          India          IND
    3  United States          USA
    

    【讨论】:

    • 感谢您的回复,但两个数据框中的国家/地区名称不同。我需要使用值的子字符串来匹配国家/地区名称。例如,美利坚合众国应该通过获取子字符串 United States 来匹配美国,并且必须相应地带来国家/地区代码。
    • 知道了。您需要导入 difflib 模块,然后运行此代码 df2.Country.map(lambda x: difflib.get_close_matches(x, df1.Country)[0]) (我已经编辑了我的原始答案)。这将在 df2 和 df1 中的 Country 列之间找到最接近的匹配项并返回。 .然后,您可以加入数据框。
    • 对不起,它不能正常工作。它为“俄罗斯”返回“突尼斯”,它不想在灾难数据框中更改我的国家名称。但是当我尝试使用下面的代码来匹配国家名称countryinfo[countryinfo['ISOen_proper'].str.contains("Russia")] 但它返回整个数据框和它苹果不能在这里传递数据框。有人可以告诉我如何为整个数据框实现此功能并将iso代码映射到灾难数据框。
    • 我修复了上面的一些代码 - 再试一次看看它是否有效。模糊匹配本质上是不完美的,因此它可能会返回一些不正确的匹配。如果您还在苦苦挣扎,请查看fuzzywuzzy 模块。
    猜你喜欢
    • 2017-12-02
    • 2016-11-27
    • 1970-01-01
    • 2020-12-28
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多