将dict值元组拆分为df标头和python3中的内容 [英] Split dict value tuples into df headers and contents in python3
本文介绍了将dict值元组拆分为df标头和python3中的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一本这样的字典:
{'Customer A': {'key1':'value 1',
'fees': [[0, 0.26], [5, 0.24], [10, 0.22], [25, 0.2],
[50, 0.18], [100, 0.16], [250, 0.14],
[500, 0.12], [1000, 0.1]],
'key3':'value3'},
{'Customer B':...
如何获得如下所示的费用" df:
How do I get a 'fees' df that looks like this:
0 5 10 25 50 100 250 500 1000
Customer A 0.26 0.24 0.22 0.2 0.18 0.16 0.14 0.12 0.1
Customer B
客户成为索引,值元组的第一部分成为标题
Customers become the Index and first part of value tuple becomes the header
仍在这里学习,因此感谢您的所有帮助.不幸的是,这感觉不像是基本的字典/列表/df操作...
Still learning here, so all help is appreciated. Unfortunately this doesn't feel like basic dictionary/list/df operations...
推荐答案
将dict comprehension
与 sort_index
:
d = {'Customer A': {'key1':'value 1',
'fees': [[0, 0.26], [5, 0.24], [10, 0.22], [25, 0.2],
[50, 0.18], [100, 0.16], [250, 0.14],
[500, 0.12], [1000, 0.1]],
'key3':'value3'}, 'Customer B': {'key1':'value 1',
'fees': [[0, 0.26], [5, 0.24], [10, 0.22], [25, 0.2],
[50, 0.18], [100, 0.16], [250, 0.14],
[500, 0.12], [1000, 0.1]],
'key3':'value3'}}
df = pd.DataFrame.from_dict({k: dict(v['fees']) for k, v in d.items()}, orient='index')
print (df)
0 50 100 5 1000 25 10 500 250
Customer A 0.26 0.18 0.16 0.24 0.1 0.2 0.22 0.12 0.14
Customer B 0.26 0.18 0.16 0.24 0.1 0.2 0.22 0.12 0.14
df = df.sort_index(axis=1)
print (df)
0 5 10 25 50 100 250 500 1000
Customer A 0.26 0.24 0.22 0.2 0.18 0.16 0.14 0.12 0.1
Customer B 0.26 0.24 0.22 0.2 0.18 0.16 0.14 0.12 0.1
这篇关于将dict值元组拆分为df标头和python3中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文