简体   繁体   English

将不均匀字典列表转换为熊猫数据框

[英]convert list of uneven dictionaries to pandas dataframe

Given this list of dictionaries鉴于此字典列表

[{'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'}}]

and this list of row names:和这个行名列表:

['Name', 'country', '30/08/2019']

How could I arrive at this DataFrame:我怎么能到达这个数据帧:

        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 ...

each Country and 30/08/2019 value would have its own cell in the DataFrame.每个 Country 和 30/08/2019 值在 DataFrame 中都有自己的单元格。 But they should be placed under each Game.但它们应该放在每个游戏之下。 Not sure if this is possible when dicts are different lengths.当字典长度不同时,不确定这是否可行。

My initial idea was to get the dicts out of the list, convert to DataFrame (somehow) in the desired way, and add the row names later.我最初的想法是将 dicts 从列表中取出,以所需的方式转换为 DataFrame(以某种方式),然后添加行名称。 I'm thinking some transposing has to find place.我在想一些移调必须找到地方。

Another idea is to make dict keys column names and go from there.另一个想法是制作 dict 键列名并从那里开始。

Eventually, this would have to be printed to an excel sheet.最终,这将不得不打印到 Excel 表格中。

I looked at previous questions , but not sure if it could apply in my case.我查看了以前的问题,但不确定它是否适用于我的情况。

You can do it as follows:你可以这样做:

# 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)

The rsult looks like:结果如下所示:

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

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

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