简体   繁体   English

通过从其他列中查找值,用字典中的键填充DataFrame列中的NaN

[英]Filling NaN in a DataFrame Column with Key from a Dictionary by looking up values from a different column

I have a dataset that looks like: 我有一个数据集,看起来像:

> Country                     Code
> 'Bolivia'                   NaN
> 'Bolivia, The Republic of'  NaN

And I also have a dictionary 我也有字典

> CountryCode = {'BOL':['Bolivia','Bolivia, The Republic of']}

How do I go on about fillna in the dataframe with the respective Key if one of the values is in the dictionary? 如果值之一在字典中,如何使用相应的键继续在数据框中填充fillna?

The desired output is 所需的输出是

> Country                     Code
> 'Bolivia'                   'BOL'
> 'Bolivia, The Republic of'  'BOL'

Thanks for your help! 谢谢你的帮助!

Create reverse dictionary of CountryCode and map it with Country column: 创建的反向字典CountryCodemap与它Country列:

new_countrycode = {v:key for key,value in CountryCode.items() for v in value}
df['Code'] = df['Country'].map(new_countrycode)

print(df)
                    Country Code
0                   Bolivia  BOL
1  Bolivia, The Republic of  BOL

print(new_countrycode)
{'Bolivia': 'BOL', 'Bolivia, The Republic of': 'BOL'}

Using .apply() 使用.apply()

df["Code"] = df.Country.apply(lambda x: ''.join(i for i, j in CountryCode.items() if x in j))

Output: 输出:

                    Country Code
0                   Bolivia  BOL
1  Bolivia, The Republic of  BOL
df=pd.DataFrame({'Country':['Bolivia','Bolivia, The Republic of'],'code':[None,None]})

Create Dataframe from dictionary of key-value code 从键值代码字典创建数据框

df_keyval=pd.DataFrame({'CountryCode':{'BOL':['Bolivia','Bolivia, The Republic of']}}).reset_index()

Match the Country and get the corresponding Key: 匹配国家/地区并获取相应的密钥:

for idx,rows in df.iterrows():
    if rows['Country'] in df_keyval.CountryCode[0]:
        df['code']=df_keyval.index[0]

Output: 输出:

    Country                    code
0   Bolivia                     BOL
1   Bolivia, The Republic of    BOL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自DataFrame的字典:来自一列的键,来自多行和多列的值(不包括NaN) - Dictionary from DataFrame: key from one column, values from multiple rows and columns excluding NaN 用上一列中的值填充 pandas dataframe 中的“无”值 - Filling up "None" values in pandas dataframe with values from previous column 用来自不同列的值填充新列 - Filling a new column with values from a different column 根据列的值在DataFrame中填充NaN - Filling NaN in DataFrame based on the values of a column 通过在字典中查找值将新列添加到数据框 - Add new column to Dataframe by looking up values in Dictionary 用来自另一个数据帧的值填充一列 - Filling a column with values from another dataframe 根据其他两列的值在一列中填充 NaN 值 - Filling in NaN values in a column depending values from two other columns 使用字典中的键值将列添加到 pandas.Dataframe - Add column to pandas.Dataframe using key values from dictionary Pandas:根据另一列的键在现有列上映射字典值以替换 NaN - Pandas: map dictionary values on an existing column based on key from another column to replace NaN Python:在匹配不同列中的值后,用来自另一个数据帧的值替换特定列中的 NaN - Python: Replacing NaN in a specific column by values from another dataframe after matching values in a different column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM