I'm trying to replace null values only in column 2 with dictionary values by matching values in column 1 with keys in dictionary.
Country Rate Tax Rate
Algeria 2.00% 76.90%
Angola 5.00% null
Algeria 2.00% null
Angola 5.00% null
Algeria 2.10% 76.90%
Angola 4.90% 52.10%
Algeria 2.20% 76.90%
- I calculated the mean Tax Rate buy Country.
- I dropped all countries that didn't have a mean calculation: all values null
- I created a dict from results {country, TR(mean)} = (k,v)
- I created code that replaces values in Tax Rate to matching country in dict
NOTE: it replaces all values in Tax Rate not just null values
tr = df.groupby('country')['tax rate'].mean()
tr.dropna(inplace=True)
tr_dict = tr.to_dict()
df['tax rate'] = df['country'].apply(lambda x: tr_dict.get(x))
results
Country Rate Tax Rate
Algeria 2.00% mean of Algeria
Angola 5.00% mean of Angola
Algeria 2.00% mean of Algeria
Angola 5.00% mean of Angola
Algeria 2.10% mean of Algeria
Angola 4.90% mean of Angola
Algeria 2.20% mean of Algeria
I believe I am missing something at the end of this code as it is correctly replacing the values by 'country' but it is replacing all 'tax rate' values and I only need it to replace null values only.
* Expected results*
Country Rate Tax Rate
Algeria 2.00% 76.90%
Angola 5.00% mean of Angola
Algeria 2.00% mean of Algeria
Angola 5.00% mean of Angola
Algeria 2.10% 76.90%
Angola 4.90% 52.10%
Algeria 2.20% 76.90%