将字典上的迭代转换为pandas数据帧

时间:2016-04-21 20:32:48

标签: python dictionary pandas dataframe

我使用条件语句(未显示)从2个输入文件生成了2个词典。然后,我打算使用这两个词典来识别重叠值。然后我想直接使用迭代的输出作为pandas数据帧。为此,我首先将迭代/ for循环输出到一个文件(Output.xls)中,然后我将该文件作为pandas dataframe读取。虽然这很好用,但我想知道是否有办法直接使用下面迭代中的'newline'作为空pandas数据帧的输入。除了dataFrame.from_dict之外,我找不到Python上的选项。但是,这需要1个字典,但我有多个字典,我正在加入,以及我正在使用的其他变量。

exp1_dict.items() is:
[('lnc3', ['SPATA1', 'AHNAK', 'FGG', 'ERAP1', 'HZ', 'SAASDAS', 'NLRC5', 'HUWE1']), ('lnc2', ['SPATA1', 'FGG', 'TMEM68', 'ATP6AP', 'HUWE1']), ('lnc1', ['SPATA1', 'AHNAK', 'FGG', 'TMEM68', 'ERAP1', 'ATP6AP', 'SAASDAS', 'RAD17', 'HUWE1'])]

exp2_dict.items() is:
[('lnc3', ['SPATA1', 'AHNAK', 'TMEM68', 'ERAP1', 'HZ', 'RAD17', 'NLRC5', 'HUWE1']), ('lnc2', ['SPATA1', 'FGG', 'ERAP1', 'HZ']), ('lnc1', ['SPATA1', 'AHNAK', 'FGG', 'TMEM68', 'ERAP1', 'HZ', 'ATP6AP', 'RAD17']), ('lnc4', ['ERAP1', 'PRSS16', 'HZ', 'NLRC5'])]

迭代字典并生成“换行符”的代码是:

out = open("Output.xls", "w") #generates an empty output file
out.write('Header1\tHeader2\tHeader3\tHeader4\tHeader5\tHeader6\tHeader7\tHeader8\tHeader9\tHeader10\tHeader11\n')#Adds header to output file

intersection_dict={} #empty intersection header
for key, value1 in exp1_dict.items(): #reiterates over the 2 dictionaries
        if key in exp2_dict.keys():
                intersection_dict[key]=list(set(value1).intersection(exp2_dict[key]))
                newline=key, str(f_exp1_dict[key]), str(f_exp2_dict[key]), str('|'.join(value1)), str(len(exp1_dict[key])), str(len(exp1_corr.index)), str('|'.join(exp2_dict[key])), str(len(exp2_dict[key])), str(len(exp2_corr.index)), str('|'.join(intersection_dict[key])), str(len(intersection_dict[key]))
                out.write('\t'.join(newline)+'\n')  

然后我使用pandas dataframe:

读取Output.xls文件
out.close()
new_input=pd.read)table("Output.xls", index_col=0)

我想知道是否有一种方法可以将上面的“换行符”写入带有上面标题的空pandas数据帧,而不是创建输出文件然后将其输入为pandas数据帧。

Output.xls文件如下所示:

Header1 Header2 Header3 Header4 Header5 Header6 Header7 Header8 Header9 Header10    Header11
lnc3    4   4   SPATA1|AHNAK|FGG|ERAP1|HZ|SAASDAS|NLRC5|HUWE1   8   12  SPATA1|AHNAK|TMEM68|ERAP1|HZ|RAD17|NLRC5|HUWE1  8   12  HZ|ERAP1|AHNAK|HUWE1|NLRC5|SPATA1   6
lnc2    2   3   SPATA1|FGG|TMEM68|ATP6AP|HUWE1  5   12  SPATA1|FGG|ERAP1|HZ 4   12  SPATA1|FGG  2
lnc1    1.5 2   SPATA1|AHNAK|FGG|TMEM68|ERAP1|ATP6AP|SAASDAS|RAD17|HUWE1    9   12  SPATA1|AHNAK|FGG|TMEM68|ERAP1|HZ|ATP6AP|RAD17   8   12  ERAP1|RAD17|AHNAK|TMEM68|ATP6AP|SPATA1|FGG  7

2 个答案:

答案 0 :(得分:1)

figuring out all the missing bits like what Alexander brought up with f_exp1_dict is time consuming and frustrating. But you might find the code below useful. You'll have to modify to include all the other strings you have.

exp1_df = pd.DataFrame([[k, v] for k, v in exp1_dict.items()], columns=['Header1', 'Header4']).set_index('Header1')
exp2_df = pd.DataFrame([[k, v] for k, v in exp2_dict.items()], columns=['Header1', 'Header7']).set_index('Header1')

newlines = pd.concat([exp1_df, exp2_df], axis=1).dropna(subset=['Header4'])

exp1_df looks like

                                                   Header4
Header1                                                   
lnc3     [SPATA1, AHNAK, FGG, ERAP1, HZ, SAASDAS, NLRC5...
lnc2                  [SPATA1, FGG, TMEM68, ATP6AP, HUWE1]
lnc1     [SPATA1, AHNAK, FGG, TMEM68, ERAP1, ATP6AP, SA...

exp2_df looks like

                                                   Header7
Header1                                                   
lnc3     [SPATA1, AHNAK, TMEM68, ERAP1, HZ, RAD17, NLRC...
lnc2                              [SPATA1, FGG, ERAP1, HZ]
lnc1     [SPATA1, AHNAK, FGG, TMEM68, ERAP1, HZ, ATP6AP...
lnc4                            [ERAP1, PRSS16, HZ, NLRC5]

newlines looks like

                                                Header4  \
lnc1  [SPATA1, AHNAK, FGG, TMEM68, ERAP1, ATP6AP, SA...   
lnc2               [SPATA1, FGG, TMEM68, ATP6AP, HUWE1]   
lnc3  [SPATA1, AHNAK, FGG, ERAP1, HZ, SAASDAS, NLRC5...   

                                                Header7  
lnc1  [SPATA1, AHNAK, FGG, TMEM68, ERAP1, HZ, ATP6AP...  
lnc2                           [SPATA1, FGG, ERAP1, HZ]  
lnc3  [SPATA1, AHNAK, TMEM68, ERAP1, HZ, RAD17, NLRC...  

答案 1 :(得分:1)

Create a list of lists which you then use to create your dataframe:

df = []
for key, value1 in exp1_dict.iteritems(): 
    if key in exp2_dict:
        dict_union = list(set(value1).intersection(exp2_dict[key]))
        col1 = key
        col2 = str(f_exp1_dict[key])
        col2 = str(f_exp2_dict[key])
        col3 = str('|'.join(value1))
        col4 = str(len(exp1_dict[key]))
        col5 = str(len(exp1_corr.index))
        col6 = str('|'.join(exp2_dict[key]))
        col7 = str(len(exp2_dict[key]))
        col8 = str(len(exp2_corr.index))
        col9 = str('|'.join(dict_union))
        col10 = str(dict_union)
        df.append([col1, col2, col3, col4, col5, col6, col7, col8, col9, col10])

df = pd.DataFrame(df)