Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split multiple dictionaries in row into new rows using Pandas

I have the following dataframe with multiple dictionaries in a list in the Rules column.

SetID      SetName             Rules
    0         Standard_1        [{'RulesID': '10', 'RuleName': 'name_abc'}, {'RulesID': '11', 'RuleName': 'name_xyz'}]   
    1         Standard_2        [{'RulesID': '12', 'RuleName': 'name_arg'}]

The desired output is:

SetID      SetName             RulesID        RuleName         
    0         Standard_1        10            name_abc
    0         Standard_1        11            name_xyz 
    1         Standard_2        12            name_arg

It might be possible that there are more than two dictionaries inside of the list.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am thinking about a pop, explode or pivot function to build the dataframe but I have no clue how to start.

Each advice will be very appreciated!

>Solution :

You can use explode:

tmp = df.explode('Rules').reset_index(drop=True)
df = pd.concat([tmp, pd.json_normalize(tmp['Rules'])], axis=1).drop('Rules', axis=1)

Output:

>>> df
   SetID     SetName RulesID  RuleName
0      0  Standard_1      10  name_abc
1      0  Standard_1      11  name_xyz
2      1  Standard_2      12  name_arg

One-liner version of the above:

df.explode('Rules').reset_index(drop=True).pipe(lambda x: pd.concat([tmp, pd.json_normalize(tmp['Rules'])], axis=1)).drop('Rules', axis=1)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading