Python 3.x 将不均匀字典列表转换为数据帧

Python 3.x 将不均匀字典列表转换为数据帧,python-3.x,pandas,list,dataframe,Python 3.x,Pandas,List,Dataframe,根据这一系列字典 [{'Empire:FourKingdoms:': {'US': '208', 'FR': '96', 'DE': '42', 'GB': '149'}}, {'BigFarmMobileHarvest:': {'US': '211', 'FR': '101', 'DE': '64', 'GB': '261'}}, {'AgeofLords:': {'US': '00', 'JP': '00', 'FR': '00', 'DE': '00', 'GB': '00'}},

根据这一系列字典

[{'Empire:FourKingdoms:': {'US': '208', 'FR': '96', 'DE': '42', 'GB': '149'}}, 
 {'BigFarmMobileHarvest:': {'US': '211', 'FR': '101', 'DE': '64', 'GB': '261'}}, 
 {'AgeofLords:': {'US': '00', 'JP': '00', 'FR': '00', 'DE': '00', 'GB': '00'}}, 
 {'BattlePiratesHQ:': {'US': '00', 'JP': '00', 'FR': '00', 'DE': '00', 'GB': '00'}},
 {'CallofWar:': {'US': '00', 'JP': '00', 'FR': '00', 'DE': '00', 'GB': '00'}}, 
 {'Empire:AgeofKnights:': {'US': '00', 'JP': '00', 'FR': '00', 'DE': '00', 'GB': '00'}}, 
 {'Empire:MillenniumWars:': {'US': '00', 'JP': '00', 'FR': '00', 'DE': '00', 'GB': '00'}}, 
 {'eRepublik:': {'US': '00', 'JP': '00', 'FR': '00', 'DE': '00', 'GB': '00'}}, 
 {'GameofEmperors:': {'US': '00', 'JP': '00', 'FR': '00', 'DE': '00', 'GB': '00'}}, 
 {'GameofTrenches:': {'US': '00', 'JP': '00', 'FR': '00', 'DE': '00', 'GB': '00'}}]
以及以下行名称列表:

['Name', 'country', '30/08/2019']
我如何才能到达这个数据帧:

        Name:    Empire:FourKingdoms  BigFarmMobileHarvest  AgeofLords     ...
0    Country:    US  FR  DE  GB       US  FR  DE  GB        US JP FR DE GB
1 30/08/2019:    208 96  42  149      211 101 64  261       00 00 00 00 00 ...
每个国家和2019年8月30日的值在数据框中都有自己的单元格。但它们应该放在每场比赛下面。 不确定当dict的长度不同时,这是否可行

我最初的想法是将dict从列表中取出,以所需的方式转换为DataFrame(以某种方式),然后添加行名。我想一定要找个地方换位

另一个想法是制作dict键列名,并从那里开始

最终,必须将其打印到excel表格中


我看了以前的文章,但不确定它是否适用于我的情况

您可以按如下方式执行:

# transform your dictionary to be flat
# so entries like 'Empire:FourKingdoms:'
# become values of key 'Name'
l2= list()
for d in l:
    for name, dct in d.items():
        dct= dict(dct)
        dct['Name']= name
        l2.append(dct)

# create a dataframe from these dictionaries
df= pd.DataFrame(l2)
# I saw you had a date in your example, so I guess you want to
# add rows from time to time
df['Date']= '30/08/2019'

# create an index based on Date and Name (the columns the data
# is aligned to) then unstack it to make Name the second
# level of the column index, swap the two levels, so Name
# is on top and finally resort the index, so the countries
# are grouped below the Name (instead of still having everything
# sorted for country so the Names appear for each country
# separately)
df.set_index(['Date', 'Name']).unstack(1).swaplevel(axis='columns').sort_index(axis=1)
结果如下所示:

Out[1]: 
Name       AgeofLords:                 BattlePiratesHQ:          ... GameofTrenches:         eRepublik:                
                    DE  FR  GB  JP  US               DE  FR  GB  ...              GB  JP  US         DE  FR  GB  JP  US
Date                                                             ...                                                   
30/08/2019          00  00  00  00  00               00  00  00  ...              00  00  00         00  00  00  00  00

天哪,它真的起作用了。我认为这是不可能的。这是一个非常棘手的问题。我也有类似的想法。熊猫可以做任何事情。几乎:-)@Vishnudev我有更多关于它们的来源:)熊猫真的很奇妙,有时也很复杂。既然你已经回答了这个问题@jottbe,介意看看我下一个关于将结果写入现有excel表的问题吗