我有两个数据帧:Disaster,CountryInfo Disaster有一个列country code,其中有一些空值,例如:
灾难:
1.**Country** - **Country_code**
2.India - Null
3.Afghanistan (the) - AFD
4.India - IND
5.United States of America - Null
国家信息:
^{pr2}$预期结果
Country Country_code
0 India IND
1 Afghanistan AFD
2 India IND
3 United States US
我需要根据国家的子字符串填写国家代码名字。可以有人建议解决这个问题吗?在
这应该行。您需要用
rename
更改列名,以便dataframes
具有相同的列名。然后,difflib
模块及其get_close_matches
方法可以对Country
名称进行模糊匹配和替换。然后就是简单地合并dataframes
下面的代码将使用
^{pr2}$df2
中的Country
列中提供最接近匹配的Country
列中的名称进行更改。这是对子串执行某种“模糊连接”的方法。在现在您可以简单地
merge
,dataframes
,它将更新df1
中丢失的Country_code
行。在相关问题 更多 >
编程相关推荐