[英]Converting a pandas dataframe to a nested dictionary with specific key
I am trying to create a nested dictionary from a pandas dataframe with the following format:我正在尝试从具有以下格式的熊猫数据框创建嵌套字典:
Name Info Location Alias
AA InfoA locationA AliasA
BB InfoB locationB AliasB
CC InfoC locationC AliasC
The result dict that I am looking for has the following format:我正在寻找的结果字典具有以下格式:
my_nested_dict = {"AA": {"Info" : "InfoA", "Location": {"Where": "locationA", "Alias": "AliasA"},
"BB": {"Info" : "InfoB", "Location": {"Where": "locationB", "Alias": "AliasB"},
"CC": {"Info" : "InfoC", "Location": {"Where": "locationC", "Alias": "AliasC"}
}
I've tried to use groupby and apply a lambda with set_index but it seems that I am doing it wrong:我尝试使用 groupby 并使用 set_index 应用 lambda,但似乎我做错了:
df.groupby('Name')[['Location', 'Alias']].apply(lambda x: x.set_index('Location').to_dict(orient='index')).to_dict()
But with this I am not able to add the "Where" key.但是有了这个,我无法添加“Where”键。 The output looks like:
输出如下所示:
my_nested_dict = {"AA": {"Info" : "InfoA", "Location": {"locationA": {"Alias": "AliasA"}},
"BB": {"Info" : "InfoB", "Location": {"locationB": {"Alias": "AliasB"}},
"CC": {"Info" : "InfoC", "Location": {"locationC": {"Alias": "AliasC"}}
}
Additionally, I've tried to first convert the columns that I am interested in, which works but I am having hard time to replace the "Location" column with the result:此外,我尝试首先转换我感兴趣的列,这很有效,但我很难用结果替换“位置”列:
df[['Location', 'Alias']].rename(columns={'Location': 'Where'}).to_dict('index')
Is there a clean pandas way to achieve the desired result?有没有一种干净的熊猫方式来达到预期的效果?
My attempt:我的尝试:
d = df.set_index("Name").to_dict("index")
for k,v in d.items():
d[k]["Location"] = {"Where": v["Location"], "Alias": v["Alias"]}
d[k].pop("Alias", None)
{'AA': {'Info': 'InfoA', 'Location': {'Where': 'locationA', 'Alias': 'AliasA'}},
'BB': {'Info': 'InfoB', 'Location': {'Where': 'locationB', 'Alias': 'AliasB'}},
'CC': {'Info': 'InfoC', 'Location': {'Where': 'locationC', 'Alias': 'AliasC'}}}
I'm wondering if there's a clean pandas solution, because the one I came up with seems too much clumsy我想知道是否有一个干净的熊猫解决方案,因为我想出的那个似乎太笨拙了
You could do:你可以这样做:
my_dict = (df.rename(columns={'Location' : 'where'})
.assign(Location = lambda df: df.apply(lambda df: {'where' : df['where'],
'Alias' : df['Alias']},
axis=1))
.set_index('Name')[['Info', 'Location']]
.to_dict('index')
)
print(my_dict)
Output输出
{'AA': {'Info': 'InfoA', 'Location': {'where': 'locationA', 'Alias': 'AliasA'}},
'BB': {'Info': 'InfoB', 'Location': {'where': 'locationB', 'Alias': 'AliasB'}},
'CC': {'Info': 'InfoC', 'Location': {'where': 'locationC', 'Alias': 'AliasC'}}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.