繁体   English   中英

将字典列表的列表转换为 Pandas DataFrame

[英]Converting a list of lists of dictionaries to a Pandas DataFrame

处理字典列表的数据结构的最佳方式可能是什么,就像我正在使用的那样:

       [[{'name': 'Export A Smooth'},
       {'filter': 'unfiltered'},
       {'number of cigarette': 25},
       {'nicotine content': 10.5},
       {'tar content': 15.0},
       {'menthol': False},
       {'king size': False},
       {'price': 18.99},
       {'units sold per week': 50},
       {'profits per week': 949.50}],

      [{'name': 'Export A Medium'},
       {'filter': 'white'},
       {'number of cigarette': 25},
       {'nicotine content': 10.0},
       {'tar content': 12.0},
       {'menthol': False},
       {'king size': False},
       {'price': 18.99},
       {'units sold per week': 39},
       {'profits per week': 740.61}],

      [{'name': 'Canadian Classics Select'},
       {'filter': 'brown'},
       {'number of cigarette': 25},
       {'nicotine content': 11.1},
       {'tar content': 11.0},
       {'menthol': True},
       {'king size': True},
       {'price': 19.09},
       {'units sold per week': 38},
       {'profits per week': 725.42}]]

并将其转换为结构化表格格式:

名称 筛选 香烟数量
导出平滑 未经过滤的 25
导出介质 白色的 25
加拿大经典 Select 棕色的 20

我已经尝试了几种不同的方法来获得正确的表格格式并且表格格式是正确的但是除了第一个(导出平滑)之外的所有香烟都会弹出很多NaN

单元 名称 筛选 每周利润
1个 导出平滑 钠盐 ... 900 钠盐
2个 钠盐 未经过滤的 ... 钠盐
3个 钠盐 钠盐 ... 钠盐
4个 钠盐 钠盐 ... 钠盐
5个 钠盐 钠盐 ... 钠盐
.. ... ... ... ...
155 钠盐 钠盐 ... 钠盐
156 钠盐 钠盐 ... 钠盐
157 钠盐 钠盐 ... 钠盐
158 钠盐 钠盐 ... 钠盐
159 钠盐 钠盐 ... 447.72

我试过pd.DataFrame(cig_list).stack().apply(pd.Series)pd.concat([pd.DataFrame(ii) for ii in cigarettes])以及遍历 cigs 并试图通过他们以这种方式进入DataFrame。

   cig_list_items = []
   for items in cig_list:
   for _ in items:
   cig_list_items.append(_)
   pd.DataFrame(cig_list_items)

它们都返回相同的结果,所以我认为字典的格式化方式一定有问题? 我怀疑字典需要重新排列,以便它们更像这样阅读:

[[{'name': 'Export A Smooth'},
  {'name': 'Export A Medium'}
  {'name': 'Pall Mall Bold'}],


  [{'filter': 'unfiltered'},
  {'filter': 'white'}
  {'filter': 'regular'}]]

由于每个条目都是一个单独的字典,您可以使用列表+字典理解加入它们:

df = pd.DataFrame([{k: v for d in i for k, v in d.items()} for i in l])

print (df)

                       name      filter  number of cigarette  nicotine content  tar content  menthol  king size  price  units sold per week  profits per week
0           Export A Smooth  unfiltered                   25              10.5         15.0    False      False  18.99                   50            949.50
1           Export A Medium       white                   25              10.0         12.0    False      False  18.99                   39            740.61
2  Canadian Classics Select       brown                   25              11.1         11.0     True       True  19.09                   38            725.42

让我们假设您的列表列表在lst变量中,然后试试这个:

flat_list = [item for sublist in t for item in lst]

df = pd.json_normalize(flat_list)

首先,它将列表的列表展平为一个列表,其中每个项目都是一个字典。 然后将整个东西转换成 pandas dataframe。

如果您发现难以理解,请阅读以下内容:

newlist=[]
for i in data:
     newdict={}
     for j in i:
         for key,item in j.items():
             new_dict[key]=item
     newlist.append(new_dict)
    
df = pd.DataFrame(newlist)

暂无
暂无

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

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