Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I scraped an election site for my state of Pennsylvania in the United States and here's the sample of my resulting nested dictionary from the site's JSON:

some_dict = {'Election': {'Statewide': [{'ADAMS': [{'CandidateName': 'BIDEN, JOSEPH '

                                                     'ROBINETTE JR',

                                    'CountyName': 'ADAMS',

                                    'ElectionDayNoVotes': '0',

                                    'ElectionDayVotes': '1',

                                    'ElectionDayYesVotes': '0',

                                    'ElectionYear': '2020'

                                    },

                                   {'CandidateName': 'TRUMP, DONALD J. ',

                                    'CountyName': 'ADAMS',

                                    'ElectionDayNoVotes': '0',

                                    'ElectionDayVotes': '1',

                                    'ElectionDayYesVotes': '0',

                                    'ElectionYear': '2020'

                                    }],

                         'ALLEGHENY': [{'CandidateName': 'BIDEN, JOSEPH '

                                                         'ROBINETTE JR',

                                        'CountyName': 'ALLEGHENY',

                                        'ElectionDayNoVotes': '0',

                                        'ElectionDayVotes': '1',

                                        'ElectionDayYesVotes': '0',

                                        'ElectionYear': '2020'

                                       },

                                       {'CandidateName': 'TRUMP, DONALD '

                                                         'J. ',

                                        'CountyName': 'ALLEGHENY',

                                        'ElectionDayNoVotes': '0',

                                        'ElectionDayVotes': '1',

                                        'ElectionDayYesVotes': '0',

                                        'ElectionYear': '2020'

                                       }]}]}}

I want it to do be displayed in a dataframe, but I am getting the output like this:

pd.DataFrame(some_dict)

Election

Statewide[{'ADAMS': [{'CandidateName': 'BIDEN, JOSEPH R...

1 Answer

0 votes
by (36.8k points)

You can use the below code:

import pandas as pd

some_dict = {'Election': {'Statewide': [{'ADAMS': [{'CandidateName': 'BIDEN, JOSEPH '

                                                     'ROBINETTE JR',

                                    'CountyName': 'ADAMS',

                                    'ElectionDayNoVotes': '0',

                                    'ElectionDayVotes': '1',

                                    'ElectionDayYesVotes': '0',

                                    'ElectionYear': '2020'

                                    },

                                   {'CandidateName': 'TRUMP, DONALD J. ',

                                    'CountyName': 'ADAMS',

                                    'ElectionDayNoVotes': '0',

                                    'ElectionDayVotes': '1',

                                    'ElectionDayYesVotes': '0',

                                    'ElectionYear': '2020'

                                    }],

                         'ALLEGHENY': [{'CandidateName': 'BIDEN, JOSEPH '

                                                         'ROBINETTE JR',

                                        'CountyName': 'ALLEGHENY',

                                        'ElectionDayNoVotes': '0',

                                        'ElectionDayVotes': '1',

                                        'ElectionDayYesVotes': '0',

                                        'ElectionYear': '2020'

                                       },

                                       {'CandidateName': 'TRUMP, DONALD '

                                                         'J. ',

                                        'CountyName': 'ALLEGHENY',

                                        'ElectionDayNoVotes': '0',

                                        'ElectionDayVotes': '1',

                                        'ElectionDayYesVotes': '0',

                                        'ElectionYear': '2020'

                                       }]}]}}

df = pd.DataFrame()

for d in some_dict['Election']['Statewide']:

    for k,v in d.items():

        t = pd.DataFrame(v)

        t['CountyName'] = k

        df = pd.concat([df,t])

Do check out data science with python certification to understand from scratch 

Browse Categories

...