简体   繁体   中英

Python Convert List of Dict Tuples into Dataframe

I have a series of Dict->List->Dict-> Tuples? that I wanted to convert into a dataframe. Ideally all at once, but even if it's just one at a time that works as well:

[OrderedDict([('clientRequestId', None),
                            ('band', 'FM'),
                            ('bandName', 'FM'),
                            ('bandType', None),
                            ('callLetters', 'WBBO'),
                            ('call_Letter_change', False),
                            ('commercial_status', 'commercial'),
                            ('countyOfLicense', None),
                            ('dmaMarketCodeOfLicense', None),
                            ('dmaMarketNameOfLicense', None),
                            ('forcedInFlags', None),
                            ('format', 'Pop Contemporary Hit Radio'),
                            ('homeToDma', False),
                            ('homeToMetro', False),
                            ('homeToTsa', False),
                            ('inTheBook', False),
                            ('metrosOfLicense', []),
                            ('name', 'WBBO-FM'),
                            ('owner', None),
                            ('qualifiedInDma', True),
                            ('qualifiedInMetro', True),
                            ('qualifiedInTsa', False),
                            ('specialActivityIndicated', False),
                            ('stateOfLicense', None),
                            ('stateOfLicenseName', None),
                            ('stationCount', 1),
                            ('stationGroup', False),
                            ('stationId', 17601)]),
               OrderedDict([('clientRequestId', None),
                            ('band', 'FM'),
                            ('bandName', 'FM'),
                            ('bandType', None),
                            ('callLetters', 'WRNB'),
                            ('call_Letter_change', False),
                            ('commercial_status', 'commercial'),
                            ('countyOfLicense', None),
                            ('dmaMarketCodeOfLicense', None),
                            ('dmaMarketNameOfLicense', None),
                            ('forcedInFlags', None), ...

I've been trying going one at a time of this:

test = pd.DataFrame.from_dict(stationDict.get('stationsInList')[0].values())
test

but the result is turning all of the values in the tuples into one column, 28 rows instead of what i wanted -1 row, 28 columns with the columns as the keys in the "tuples".

You can create dataframe by just giving the list of dicts.

data = [OrderedDict([('clientRequestId', None), ('band', 'FM'), ('bandName', 'FM'), ('bandType', None), ('callLetters', 'WBBO'), ('call_Letter_change', False), ('commercial_status', 'commercial'), ('countyOfLicense', None), ('dmaMarketCodeOfLicense', None), ('dmaMarketNameOfLicense', None),('forcedInFlags', None),('format', 'Pop Contemporary Hit Radio'),('homeToDma', False),('homeToMetro', False),('homeToTsa', False),('inTheBook', False),('metrosOfLicense', []),('name', 'WBBO-FM'),('owner', None),('qualifiedInDma', True),('qualifiedInMetro', True),('qualifiedInTsa', False),('specialActivityIndicated', False),('stateOfLicense', None),('stateOfLicenseName', None),('stationCount', 1),('stationGroup', False),('stationId', 17601)])]
df = pd.DataFrame(data)

Output:

  clientRequestId band bandName  ... stationCount stationGroup  stationId
0            None   FM       FM  ...            1        False      17601

[1 rows x 28 columns]

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM