Python将Dict元组列表转换为数据框

时间:2020-04-23 18:38:51

标签: python pandas

我有一系列Dict-> List-> Dict->元组?我想转换成一个数据框。理想情况是一次全部,但即使一次只工作一次也很好:

[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), ...

我一直在尝试一次:

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

但是结果是将元组中的所有值都变成一列,而不是我想要的-1行,而是28行,其中28列是“元组”中的键。

1 个答案:

答案 0 :(得分:0)

您可以通过提供字典列表来创建数据框。

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)

输出:

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

[1 rows x 28 columns]