python - Get a list of keys and values in a nested dictionary oriented by index -
i have excel file structure this:
name age status anna 35 single petr 27 married i have converted such file nested dictionary structure this:
{'anna': {'age':35}, {'status': 'single'}}, {'petr': {'age':27}, {'status': 'married'}} using pandas:
import pandas pd df = pd.read_excel('path/to/file') df.set_index('name', inplace=true) print(df.to_dict(orient='index')) but when running list(df.keys()) returns me list of keys in dictionary ('age', 'status', etc) not 'name'.
my eventual goal returns me keys , values typing name.
is possible somehow? or maybe should use other way import data in order achieve goal? should anyway come dictionary because merge other dictionaries key.
i think need parameter drop=false set_index not drop column name:
import pandas pd df = pd.read_excel('path/to/file') df.set_index('name', inplace=true, drop=false) print (df) name age status name anna anna 35 single petr petr 27 married d = df.to_dict(orient='index') print (d) {'anna': {'age': 35, 'status': 'single', 'name': 'anna'}, 'petr': {'age': 27, 'status': 'married', 'name': 'petr'}} print (list(df.keys())) ['name', 'age', 'status']
Comments
Post a Comment