简体   繁体   English

Python 将字典元组列表转换为 Dataframe

[英]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.我想转换成 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".但结果是将元组中的所有值变成一列,28行而不是我想要的-1行,28列,列作为“元组”中的键。

You can create dataframe by just giving the list of dicts.您只需提供字典列表即可创建 dataframe 。

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: Output:

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

[1 rows x 28 columns]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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