【发布时间】:2021-12-31 01:40:03
【问题描述】:
我有一个嵌套字典annot_dict,其结构:
- key = 长唯一字符串
- 值 = 字典列表
值,字典列表,每个都有结构:
- key = 长唯一字符串(上层字典键的子类别)
- 值 = 五个字符串项的列表
整个结构的一个例子是:
annot_dict['ID_string'] = [
{'ID_string': ['attr1a', 'attr1b', 'attr1c', 'attr1d', 'attr1e']},
{'string2' : ['attr2a', 'attr2b', 'attr2c', 'attr2d', 'attr2e']},
{'string3' : ['attr3a', 'attr3b', 'attr3c', 'attr3d', 'attr3e']},
]
ID_string 与第一个子字典键相同。这是我编写的 gff3 文件解析器函数的输出,真正的字典信息是来自人类 9 号染色体基因组的基因 (ID_string) 和转录本 (string2, string3,...),如果有人的话熟悉该文件类型的结构。属性列表描述了生物型、起始索引、结束索引、链和描述。
我现在想将这些信息放入 pandas DataFrame 中。我想遍历 dict 中最外层的键(ID_strings),以创建一个大 DataFrame,其中包含每个 ID_string 的行和它下面的每个子类别的行(string2、string3)。
我希望它看起来像这样:
| subunit_ID | gene_ID | start_index | end_index | strand |biotype | desc |
|------------|-----------|-------------|-----------|--------|--------|--------|
|'ID_string' |'ID_string'| 'attr1a' | 'attr1b' |'attr1c'|'attr1d'|'attr1e'|
| 'string2' |'ID_string'| 'attr2a' | 'attr2b' |'attr2c'|'attr2d'|'attr2e'|
| 'string3' |'ID_string'| 'attr3a' | 'attr3b' |'attr3c'|'attr3d'|'attr3e'|
我确实看过other answers,但没有一个与我的字典结构完全相同。这是我关于 SO 的第一个问题,因此请随时提高我的问题的可理解性。
【问题讨论】:
-
如何提供'subunit_ID'、'gene_ID'、'start_index'、'end_index'、'strand'、'biotype'、'desc'? “gene_ID”是如何填写的?你尝试了什么?
标签: python pandas dataframe dictionary