I have a dataframe like this:
Subject_id Subject Score
Subject_1 Math 5
Subject_1 Language 4
Subject_1 Music 8
Subject_2 Math 8
Subject_2 Language 3
Subject_2 Music 9
And I want to convert it into a dictionary, grouping by subject_id
{'Subject_1': {'Math': 5,
'Language': 4,
'Music': 8},
{'Subject_2': {'Math': 8,
'Language': 3,
'Music': 9}
}
If I would have only one Subject, then I could so:
my_dict['Subject_1'] = dict(zip(df['Subject'],df['Score']))
But since I have several subjects the list of keys repeats, so I cannot use directly a zip.
Dataframes has a .to_dict('index')
method but I need to be able to group by a certain column when creating the dictionary.
How could I achieve that?
Thanks.
Use groupby
with custom lambda function and last convert output Series
to_dict
:
d = (df.groupby('Subject_id')
.apply(lambda x: dict(zip(x['Subject'],x['Score'])))
.to_dict())
print (d)
{'Subject_2': {'Math': 8, 'Music': 9, 'Language': 3},
'Subject_1': {'Math': 5, 'Music': 8, 'Language': 4}}
Detail:
print (df.groupby('Subject_id').apply(lambda x: dict(zip(x['Subject'],x['Score']))))
Subject_id
Subject_1 {'Math': 5, 'Music': 8, 'Language': 4}
Subject_2 {'Math': 8, 'Music': 9, 'Language': 3}
dtype: object
Use to_dict
with pivot
In [29]: df.pivot('Subject_id', 'Subject', 'Score').to_dict('index')
Out[29]:
{'Subject_1': {'Language': 4L, 'Math': 5L, 'Music': 8L},
'Subject_2': {'Language': 3L, 'Math': 8L, 'Music': 9L}}
Or,
In [25]: df.set_index(['Subject_id', 'Subject']).unstack()['Score'].to_dict('index')
Out[25]:
{'Subject_1': {'Language': 4L, 'Math': 5L, 'Music': 8L},
'Subject_2': {'Language': 3L, 'Math': 8L, 'Music': 9L}}
Adding to Zero that you can use asterisk (*) for more comfort and or additional filtering via list comprehension of df.columns
import io
import pandas as pd
TESTDATA = """
Subject_id; Subject; Score
Subject_1; Math; 5
Subject_1; Language; 4
Subject_1; Music; 8
Subject_2; Math; 8
Subject_2; Language; 3
Subject_2; Music; 9
"""
df = pd.read_csv( io.StringIO(TESTDATA) , sep=";").applymap(lambda x: x.strip() if isinstance(x, str) else x)
df.pivot(*df.columns).to_dict('index')
{'Subject_1': {'Language': 4, 'Math': 5, 'Music': 8},
'Subject_2': {'Language': 3, 'Math': 8, 'Music': 9}}
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.