这本词典的格式很奇怪
运行嵌套for循环时,它会中断,因为某些键'top'
和'rising'
带有“None”
实际上在中有可用数据的索引有一些噪声,例如文本: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
IIUC,您可以执行
concat
:输出:
相关问题 更多 >
编程相关推荐