I have DataFrameas follows:
In [23]: df = pandas.DataFrame({'Initial': ['C','A','M'], 'Sex': ['M', 'F', 'F'], 'Age': [49, 39, 19]})
df = df[['Initial', 'Sex', 'Age']]
df
Out[23]:
Initial Sex Age
0 C M 49
1 A F 39
2 M F 19
My goal is to create such a voice recorder:
{'C': ('49', 'M'), 'A': ('39', 'F'), 'M': ('19', 'F')}
I am currently doing this:
In [24]: members = df.set_index('FirstName', drop=True).to_dict('index')
members
Out[24]: {'C': {'Age': '49', 'Sex': 'M'}, 'A': {'Age': '39', 'Sex': 'F'}, 'M': {'Age': '19', 'Sex': 'F'}}
Then I use dictcomprehrension to format the key values as tuples instead of dicts:
In [24]: members= {x: tuple(y.values()) for x, y in members.items()}
members
Out[24]: {'C': ('49', 'M'), 'A': ('39', 'F'), 'M': ('19', 'F')}
My question is: is there a way to get dictin the format I want from pandas DataFramewithout calling an additional subroutine of understanding dict?