我有一個annot_dict帶有結構的嵌套字典:
- 鍵 = 長唯一字串
- 值 = 字典串列
值,字典串列,每個都有結構:
- 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 中。我想遍歷ID_stringdict 中最外層的鍵(s)以制作一個大的 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'|
我確實看過其他答案,但沒有一個答案與我的 dict 結構完全相同。這是我關于 SO 的第一個問題,所以請隨時提高我的問題的可理解性。提前致謝。
uj5u.com熱心網友回復:
你可以這樣做:
df = pd.DataFrame(
(
[subkey, key] value
for key, records in annot_dict.items()
for record in records
for subkey, value in record.items()
),
columns=[
'subunit_ID', 'gene_ID', 'start_index', 'end_index', 'strand','biotype', 'desc'
]
)
結果為
annot_dict = {
'ID_string1': [
{'ID_string1': ['attr11a', 'attr11b', 'attr11c', 'attr11d', 'attr11e']},
{'string12' : ['attr12a', 'attr12b', 'attr12c', 'attr12d', 'attr12e']},
{'string13' : ['attr13a', 'attr13b', 'attr13c', 'attr13d', 'attr13e']},
],
'ID_string2': [
{'ID_string2': ['attr21a', 'attr21b', 'attr21c', 'attr21d', 'attr21e']},
{'string22' : ['attr22a', 'attr22b', 'attr22c', 'attr22d', 'attr22e']},
{'string23' : ['attr23a', 'attr23b', 'attr23c', 'attr23d', 'attr23e']},
]
}
是
subunit_ID gene_ID start_index end_index strand biotype desc
0 ID_string1 ID_string1 attr11a attr11b attr11c attr11d attr11e
1 string12 ID_string1 attr12a attr12b attr12c attr12d attr12e
2 string13 ID_string1 attr13a attr13b attr13c attr13d attr13e
3 ID_string2 ID_string2 attr21a attr21b attr21c attr21d attr21e
4 string22 ID_string2 attr22a attr22b attr22c attr22d attr22e
5 string23 ID_string2 attr23a attr23b attr23c attr23d attr23e
uj5u.com熱心網友回復:
您可以使用串列理解將 dicts 扁平化為包含 dict 鍵作為專案的串列,然后將其加載到 Pandas:
import pandas as pd
annot_dict = {}
annot_dict['ID_string'] = [
{'ID_string': ['attr1a', 'attr1b', 'attr1c', 'attr1d', 'attr1e']},
{'string2' : ['attr2a', 'attr2b', 'attr2c', 'attr2d', 'attr2e']},
{'string3' : ['attr3a', 'attr3b', 'attr3c', 'attr3d', 'attr3e']},
]
df = pd.DataFrame([[k] list(annot_dict['ID_string'][0].keys()) v for i in annot_dict['ID_string'] for k, v in i.items()], columns=['subunit_ID','gene_ID','start_index','end_index','strand','biotype','desc'])
輸出:
| 子單元_ID | 基因ID | 起始索引 | 結束索引 | 鏈 | 生物型 | 描述 | |
|---|---|---|---|---|---|---|---|
| 0 | ID_string | ID_string | attr1a | 屬性1b | attr1c | 屬性1d | 屬性1e |
| 1 | 字串 2 | ID_string | attr2a | attr2b | attr2c | attr2d | 屬性 |
| 2 | 字串 3 | ID_string | attr3a | attr3b | attr3c | attr3d | attr3e |
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/362565.html
上一篇:字數分布熊貓資料框
