如何从给定的数据框架创建嵌套词典?
我得到了这样的DF:
level profile chest_gold chest_silver chest_bronze
1 a TRUE FALSE TRUE
2 a FALSE FALSE TRUE
3 a FALSE TRUE TRUE
我想获得一本用作关键级别和配置文件的字典来返回这样的内容,在1/0中转换了True/False:
d[profile][level] = [1, 0, 1] #the chest result
例如:
d['a'][1] = [1,0,1]
d['a'][2] = [0,0,1]
d['a'][3] = [0,1,1]
我该怎么做?
PS,如果您留下解决方案,请留下一些评论来解释答案!
I got a df like this one:
level profile chest_gold chest_silver chest_bronze
1 a TRUE FALSE TRUE
2 a FALSE FALSE TRUE
3 a FALSE TRUE TRUE
I want to obtain a dictionary which uses as key the level and the profile to return something like this, converting the TRUE/FALSE in 1/0:
d[profile][level] = [1, 0, 1] #the chest result
for example:
d['a'][1] = [1,0,1]
d['a'][2] = [0,0,1]
d['a'][3] = [0,1,1]
How can I do that?
P.s. if you leave a solution please leave also a little comment to explain the answer!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查嵌套词典理解。内部理解构建词典,其中键是级别,外部理解构建了一个词典,其中键是剖面。
Check a nested dictionary comprehension. The inner comprehension builds dictionary where keys are levels and outer comprehension builds a dictionary where the keys are profiles.