简体   繁体   English

Pandas DataFrame 按两列分组并在相同的键插入列表中获得不同的关系

[英]Pandas DataFrame Groupby two columns and get different relation in same keys insert list

I have this table:我有这张桌子:

  Head  Relation    Tail 
0   9       1          0 
1   10      1         11  
2   9       0         23 
3   10      1         61 
4   9       0         12
5   10      0         66
6   10      0         61

I have to create a dictionary with Head key and values equal to the relations but not repeated and for each value of the relations I have to insert the corresponding tail.我必须创建一个字典,其 Head 键和值等于关系但不重复,并且对于关系的每个值,我必须插入相应的尾部。

example:例子:

{9 : {1: [10], 0:[23,12]}, 10 : {1:[11,61], 0:[66,61]}}

I don't really know how to do it.我真的不知道该怎么做。 Is there someone who can help me?有人可以帮助我吗?

结果与南。

Second Example Input:第二个示例输入:

    Head    Relation    Tail  
  0 207       1         269  
  1 207       1          61  
  2 207       0          62  
  3 208       1          269 
  4 290       0          269

the output: output:

{207: {0: [62], 1: [269,61]}, 208: {0: nan, 1: [269]},
 290: {0: [269], 1: nan}}

I would like to remove the nans我想删除 nans

You could use pivot_table and to_dict :您可以使用pivot_tableto_dict

(df.pivot_table(index='Head', columns='Relation', values='Tail', aggfunc=list)
   .to_dict('index')
)

Or, the other way around:或者,反过来:

(df.pivot_table(index='Relation', columns='Head', values='Tail', aggfunc=list)
   .to_dict()
)

output: output:

{9: {0: [23, 12], 1: [0]}, 10: {0: [66, 61], 1: [11, 61]}}
post-processing the output to remove NaNs:对 output 进行后处理以删除 NaN:
d = (df.pivot_table(columns='Head', index='Relation', values='Tail', aggfunc=list)
     .to_dict()
     )

d2 = {k: {k2:v2 for k2,v2 in v.items() if pd.isna(v2) is not True}
      for k,v in d.items()}

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

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