简体   繁体   English

字典中的熊猫数据框本身在字典中

[英]Pandas dataframes in a dictionary itself in a dictionary

I have a large dataframe, here's an extract:我有一个大数据框,这是一个摘录:

Index指数 Protocol ID协议 ID Activity ID活动编号 Detail 1细节 1 Detail 2细节 2
0 0 1509 1509 15 15 a一个 s s
1 1 636 636 159 159 b b t
2 2 787 787 159 159 c C u
3 3 796 796 159 159 d d v v
4 4 1174 1174 159 159 e e w w
5 5 787 787 252 252 f F x X
6 6 1029 1029 252 252 g G y是的
7 7 1188 1188 NaN h H z z
8 8 1848 1848年 NaN i一世 a一个
9 9 TBD待定 NaN j j b b
10 10 TBD待定 NaN k ķ c C
11 11 1029 1029 253 253 l l d d
12 12 1170 1170 253 253 m e e
13 13 1468 1468 NaN n n f F
14 14 957 957 NaN o g G
15 15 1029 1029 254 254 p p h H
16 16 957 957 254 254 q q i一世
17 17 841 841 166 166 r r j j

I need to create a dictionary of dictionaries named by Activity ID.我需要创建一个由 Activity ID 命名的字典。 In each "activity dictionary", I need all the dataframes named by the protocol ID and in each of the protocol dataframe, I need the complete informations from all the initial columns.在每个“活动字典”中,我需要由协议 ID 命名的所有数据帧,并且在每个协议数据帧中,我需要来自所有初始列的完整信息。

So, dictionary of activities → In each one, dictionary of their protocols → In each one, dataframe of all the information linked to the protocol所以,活动字典 → 在每一个中,它们的协议字典 → 在每一个中,与协议相关的所有信息的数据框

When I code当我编码时

activity_dict = initial_dataframe.set_index('Activity ID').T.to_dict('dict')

It does create me a dictionary named by activity ID but when I click on an activity, it only shows the last of the protocols and the information linked to it.它确实为我创建了一个由活动 ID 命名的字典,但是当我单击一个活动时,它只显示最后一个协议和链接到它的信息。 Since this is basically doing the last step I need, I'm trying to add the intermediate step which is the protocols dataframes which should appear when I click on an activity dictionary or run for example:因为这基本上是在做我需要的最后一步,所以我试图添加中间步骤,即当我单击活动字典或运行时应该出现的协议数据帧,例如:

activity_dict["159"]

Which right now is showing me现在正在向我展示

{'Protocol ID': '1174',
 'Protocol Name': 'My analyses',
 'I/O': 'O',
 'Prot Owner': 'lorem.ipsum',
 'Notes': 'Done',
 'Activity Name': 'Test',
 'Comments': nan,
 'Activity Owner': nan,
 'Protocol description': nan,
 'Fonte': nan}  

When I would like for it to show me a link to the dataframes linked to not only protocol 1174, but also 636 and 787.当我想让它向我显示一个链接到不仅链接到协议 1174,还链接到 636 和 787 的数据帧时。

Does anybody know how to do this?有人知道怎么做这个吗?

Thank you in advance先感谢您

Because dictionary has unique keys one possible solution is create lists for all values:因为字典具有唯一键,一种可能的解决方案是为所有值创建列表:

activity_dict = initial_dataframe.groupby('Activity ID').agg(list).to_dict('index')
print (activity_dict)

{'15': {'Protocol ID': ['1509'], 'Detail 1': ['a'], 'Detail 2': ['s']}, 
 '159': {'Protocol ID': ['636', '787', '796', '1174'], 'Detail 1': ['b', 'c', 'd', 'e'], 'Detail 2': ['t', 'u', 'v', 'w']}, 
 '166': {'Protocol ID': ['841'], 'Detail 1': ['r'], 'Detail 2': ['j']}, 
 '252': {'Protocol ID': ['787', '1029'], 'Detail 1': ['f', 'g'], 'Detail 2': ['x', 'y']}, 
 '253': {'Protocol ID': ['1029', '1170'], 'Detail 1': ['l', 'm'], 'Detail 2': ['d', 'e']}, 
 '254': {'Protocol ID': ['1029', '957'], 'Detail 1': ['p', 'q'], 'Detail 2': ['h', 'i']}}

Or use join:或使用加入:

activity_dict = initial_dataframe.groupby('Activity ID').agg(','.join).to_dict('index')
print (activity_dict)

{'15': {'Protocol ID': '1509', 'Detail 1': 'a', 'Detail 2': 's'}, 
 '159': {'Protocol ID': '636,787,796,1174', 'Detail 1': 'b,c,d,e', 'Detail 2': 't,u,v,w'}, 
 '166': {'Protocol ID': '841', 'Detail 1': 'r', 'Detail 2': 'j'}, 
 '252': {'Protocol ID': '787,1029', 'Detail 1': 'f,g', 'Detail 2': 'x,y'}, 
 '253': {'Protocol ID': '1029,1170', 'Detail 1': 'l,m', 'Detail 2': 'd,e'}, 
 '254': {'Protocol ID': '1029,957', 'Detail 1': 'p,q', 'Detail 2': 'h,i'}}

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

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