简体   繁体   中英

How can I fill a dataframe from a recursive dictionary values?

I have created a script that allows me to read multiple pdf files and extract information recursively one by one. This script generates a dictionary with data by pdf. Ex: 1º Iteration from 1º PDF file:

d = {"GGT":["transl","mut"], "ATT":["alt3"], "ATC":["alt5"], "AUC":["alteration"]}

2º In the Second Iteration from 2º PDF file:

d = {"GGT":["transl","mut"], "AUC":["alteration"]}

. . . Doing this until 200 pdf files.

Initially I have a dataframe created with all the genes that allow to detect that analysis.

df = pd.DataFrame(data=None, columns=["GGT","AUC","ATC","ATT","UUU","UUT"], dtype=None, copy=False)

Desire output: What I would like to obtain is a dataframe where the information of the values is stored in a recursive way line by line. For example:

在此处输入图像描述

Is there an easy way to implement this? or functions that can help me?

IIUC, you are trying to loop through the dictionaries and add them as rows in your dataframe? I'm not sure how this applies to recursion with "What I would like to obtain is a dataframe where the information of the values is stored in a recursive way line by line."

d1 = {"GGT":["transl","mut"], "ATT":["alt3"], "ATC":["alt5"], "AUC":["alteration"]}
d2 = {"GGT":["transl","mut"], "AUC":["alteration"]}
dicts = [d1, d2] #imagine this list contains the 200 dictionaries
df = pd.DataFrame(data=None, columns=["GGT","AUC","ATC","ATT","UUU","UUT"], dtype=None, copy=False)
for d in dicts: #since only 200 rows a simple loop with append
    df = df.append(d, ignore_index=True)
df
Out[1]: 
             GGT           AUC     ATC     ATT  UUU  UUT
0  [transl, mut]  [alteration]  [alt5]  [alt3]  NaN  NaN
1  [transl, mut]  [alteration]     NaN     NaN  NaN  NaN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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