简体   繁体   中英

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:

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. 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'}}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM