如何将 Pandas 数据帧转换为 defaultdict(类,列表),其中列值之一用作键?

eve*_*007 4 python list-comprehension dataframe pandas defaultdict

在给定的熊猫数据框中:

df = 

     contig       pos  PI_index  hapX_My_Sum  hapY_My_Sum  hapX_Sp_Sum       
 0  2  16229767           726          0.0         12.0          3.5   
 1  2  16229783           726          0.0         12.0          3.5   
 3  2  16229880           726          0.0         12.0          2.0   
 4  2  16230491           255         12.0          0.0          0.0   
 5  2  16230503           255         12.0          0.0          0.0   
 6  2  16232072           255         11.0          1.0          0.0   
 7  2  16232072           255         11.0          1.0          0.0   
 8  2  16232282          3353         11.0          1.0          0.0   
 9  2  16232444          3353         11.0          1.0          0.0   
 10 2  16232444          3353         11.0          1.0          0.0   
Run Code Online (Sandbox Code Playgroud)

我想这个数据帧转换为dictionary of dictionarydefault(dict)

所以我做了:

from collections import defaultdict
df_dict = df.to_dict('index')

print(df_dict)  # gives me
{0: {'hapY_My_Sum': 12.0, 'hapX_Sp_Sum': 3.5 .....}
Run Code Online (Sandbox Code Playgroud)

All, is good but not using the main pandas indexI want to use the PI_indexas the index to generate defaultdict(<class 'dict'>where PI_indexvalues are thekeys进行下游分析。

的打印输出defaultdict应该是这样的:

defaultdict(<class 'dict'>, {'726': {'contig': '2', 'hapX_My_Sum': ['0.0', '0.0', '0.0'], 'hapY_My_Sum': ['12.0', '12.0', '12.0'], ....}, '255':{'contig': '2', 'hapX_My_Sum': [....]....}})

帖子编辑:

  • 我忘了添加,但是如果不需要,有没有办法取消选择某些列,但我不想将它们从 Pandas 数据框中删除。
  • 另外,如果我只想要contig 中的一个值怎么办,因为它们都是相同的。

所以,下游我可以做这样的事情:

for k in df_dict:
    contig = df_dict[k]['chr']

    hapX_My_product = reduce(mul, (float(x) for x in (df_dict[k]['hapX_My_Sum'])))
Run Code Online (Sandbox Code Playgroud)

Max*_*axU 5

那是你要的吗?

In [11]: cols = ['contig','PI_index','hapX_My_Sum']

In [12]: df[cols].groupby('PI_index') \
                 .apply(lambda x: x.set_index('PI_index').to_dict('list')) \
                 .to_dict()
Out[12]:
{255: {'contig': [2, 2, 2, 2], 'hapX_My_Sum': [12.0, 12.0, 11.0, 11.0]},
 726: {'contig': [2, 2, 2], 'hapX_My_Sum': [0.0, 0.0, 0.0]},
 3353: {'contig': [2, 2, 2], 'hapX_My_Sum': [11.0, 11.0, 11.0]}}
Run Code Online (Sandbox Code Playgroud)

一些解释:

首先我们为每个组生成字典

In [87]: df[cols].groupby('PI_index') \
    ...:         .apply(lambda x: x.set_index('PI_index').to_dict('list'))
Out[87]:
PI_index
255     {'contig': [2, 2, 2, 2], 'hapX_My_Sum': [12.0,...
726     {'contig': [2, 2, 2], 'hapX_My_Sum': [0.0, 0.0...
3353    {'contig': [2, 2, 2], 'hapX_My_Sum': [11.0, 11...
dtype: object
Run Code Online (Sandbox Code Playgroud)

现在我们可以将行导出为字典,设置相应的索引并使用默认值 orient='dict'

In [88]: df[cols].groupby('PI_index') \
    ...:         .apply(lambda x: x.set_index('PI_index').to_dict('list')) \
    ...:         .to_dict()
Out[88]:
{255: {'contig': [2, 2, 2, 2], 'hapX_My_Sum': [12.0, 12.0, 11.0, 11.0]},
 726: {'contig': [2, 2, 2], 'hapX_My_Sum': [0.0, 0.0, 0.0]},
 3353: {'contig': [2, 2, 2], 'hapX_My_Sum': [11.0, 11.0, 11.0]}}
Run Code Online (Sandbox Code Playgroud)