python-3.x - 将不均匀字典列表转换为 pandas 数据框

标签 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'}}, 
 {'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']

我怎样才能得到这个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 ...

每个国家/地区和 30/08/2019 值将在 DataFrame 中拥有自己的单元格。但它们应该放在每个游戏下面。 不确定当字典长度不同时这是否可能。

我最初的想法是将字典从列表中取出,以所需的方式转换为 DataFrame(以某种方式),然后添加行名称。我认为一些转调必须找到地方。

另一个想法是创建字典键列名称并从那里开始。

最终,这必须打印到 Excel 工作表中。

我查看了之前的questions ,但不确定它是否适用于我的情况。

最佳答案

您可以按如下方式进行操作:

# 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

关于python-3.x - 将不均匀字典列表转换为 pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57744355/

相关文章:

python - python自动添加空格

python Pandas : Append rows of DataFrame and delete the appended rows

python - 如何使 matplotlib/pandas 条形图看起来像直方图?

android - 在 Activity 中嵌入一个大小的 ListView - android

swift - 如何根据 SwiftUI 中的日期将事件分成列表的不同部分?

python-3.x - 具有重复组名的从长到宽的转换

python - tensorflow 中 `*` 记录在哪里?

python - 我应该使用 Python casefold 吗?

python - Pandas to_latex() 转义数学模式

c++ - 为什么插入到堆中比插入到未排序的列表中更快?