Filling a series based on key value pairs

主宰稳场 提交于 2019-12-23 22:12:43

问题


I have a data frame that includes a dictionary of codes and names. Some of the names are blank. I'm trying to fill those blanks based on other examples where the code matches a name. Thus far I have sorted into a df by code but can't figure out how to "fill in the blanks". Here is my code:

import pandas as pd
import json
from pandas.io.json import json_normalize
df = pd.read_json('data/world_bank_projects.json')
theme= pd.DataFrame([val for pair in df['mjtheme_namecode'].values for val in pair])
theme1 = theme.set_index('code').sort_index()
print(theme1)

                     name
code                     
1     Economic management
1     Economic management
1     Economic management
1     Economic management
1     Economic management
1     Economic management
1     Economic management
1     Economic management
1     Economic management
1     Economic management
1                        
1     Economic management
1                        
1     Economic management
1     Economic management
1                        
1     Economic management
1     Economic management
1     Economic management
1     Economic management

After I fill in I'd like to count (by either code or name which should be the same). I think I could just use value_counts for that, right?

来源:https://stackoverflow.com/questions/51754475/filling-a-series-based-on-key-value-pairs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!