【问题标题】:How extract records from a dictionary that doesn't have comma and with None values in the keys如何从没有逗号且键中没有值的字典中提取记录
【发布时间】:2020-08-29 00:10:08
【问题描述】:

这本词典的格式很奇怪。

  • 在运行嵌套 for 循环时,它会中断,因为 'top''rising' 的某些键带有“无”

  • 实际上有可用数据的索引,有一些噪音,例如文本:query value或不是索引的文本数字,例如0 1 2 3

  • 也没有逗号来分隔行。

所以目标是......将数据的可用部分转换为数据框。

数据:

d = 

{1: {'abroad': {'top': None, 'rising': None}},
 2: {'house': {'top': None, 'rising': None}},
 3: {'school': {'top':                            query  value
   0     l    100
   1     x    100
   2     y     44
   3     j     31
   4     k      6, 'rising': None}},
 4: {'in_house': {'top':                            query  value
   0            a    100
   1            b     97
   2            c     32
   3            d     12,  'rising': None}},
 5: {'community': {'top': None, 'rising':      query  value
   0            s    100}},
 }

我的代码:

list_words = []


for x in dicti:

    for a in dicti[x]:
        print(x, a)

        for b in dicti[x][a].values():
            print(b)
            list_words.append(b)



data = pd.DataFrame(list_words)
data = data.dropna(how='all')  
data = data.rename(columns={0:'search'})
data = data.search.astype(str)
data = data.reset_index()

data = data[0].str.split(",")


期望的输出:

search     score    status
l        100      top
x        100      top
y        44       top 
j        31       top
k        6        top
a        100      top
b        97       top
c        32       top
d        12       top
s        100      rising

【问题讨论】:

  • 您的字典无效
  • 谢谢你们,下面的答案很有效,太好了。看看
  • 它没有任何意义,只是用随机词替换了真实值,以保持问题的某种模式
  • 这将有助于通过了解数据的形式来解决问题。奇怪的值是数据框 df1 = pd.DataFrame({'query': ['l', 'x'], 'value': [100, 100]}) df2 = pd.DataFrame({'query': ['a', 'b'], 'value': [100, 97]}) df3 = pd.DataFrame({'query': ['s'], 'value': [100]})data = {1: {'abroad': {'top': None, 'rising': None}}, 2: {'house': {'top': None, 'rising': None}}, 3: {'school': {'top': df1, 'rising': None}}, 4: {'in_house': {'top': df2, 'rising': None}}, 5: {'community': {'top': None, 'rising': df3}}} 如果您打印 data,它将看起来像您的示例。

标签: python json pandas dictionary


【解决方案1】:

你可以concat:

pd.concat(pd.DataFrame(v).assign(status=k) for y in d.values() 
            for x in y.values() for k,v in x.items()
         )

输出:

   status query  value
0     top     l  100.0
1     top     x  100.0
2     top     y   44.0
3     top     j   31.0
4     top     k    6.0
0     top     a  100.0
1     top     b   97.0
2     top     c   32.0
3     top     d   12.0
0  rising     s  100.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-23
    相关资源
    最近更新 更多