简体   繁体   English

如何将作为列表列表值的字典转换为python中的数据框?

[英]How to convert dictionary which as values as list of lists into dataframe in python?

I have a dictionary like this., keys as 'Start postions' and values as list of entries, each entry contains multiple other values. 我有一个这样的字典,键作为“开始位置”,值作为条目列表,每个条目都包含多个其他值。

dict1 = {28878779: 
[[0.63078648931418,'BRCA','Primary Blood Derived Cancer','chr16'],
  [0.913319324289701, 'BRCA', 'Primary Blood Derived Cancer', 'chr16'],
  [0.4291909025802871, 'BRCA', 'Primary Blood Derived Cancer', 'chr16'],
  [0.7571498628201009, 'BRCA', 'Primary Blood Derived Cancer', 'chr16'],
  [0.20053355013001398, 'BRCA', 'Primary Blood Derived Cancer', 'chr16'],
  [0.47222708511173905, 'BRCA', 'Primary Blood Derived Cancer', 'chr16'],
  [0.5421979810611359, 'BRCA', 'Primary Blood Derived Cancer', 'chr16'],
  [0.517080694962231, 'BRCA', 'Primary Blood Derived Cancer', 'chr16'],
  [0.354578922865826, 'BRCA', 'Primary Blood Derived Cancer', 'chr16'],
  [0.47933127476003706, 'BRCA', 'Primary Blood Derived Cancer', 'chr16']]
116276795: 
[[0.0295335249313507,'BRCA','Primary Blood Derived Cancer','chr12'],
  [0.0225709542480921, 'BRCA', 'Primary Blood Derived Cancer', 'chr12'],
  [0.0230930552162406, 'BRCA', 'Primary Blood Derived Cancer', 'chr12'],
  [0.0226794373583645, 'BRCA', 'Primary Blood Derived Cancer', 'chr12'],
  [0.0465238706721383, 'BRCA', 'Primary Blood Derived Cancer', 'chr12'],
  [0.0308525159082739, 'BRCA', 'Primary Blood Derived Cancer', 'chr12'],
  [0.0280263565564701, 'BRCA', 'Primary Blood Derived Cancer', 'chr12']]
...}

I want to convert the dictionary into dataframe like this., A dataframe which contains dictionary's keys and values (each entry of the values) into rows of dataframe. 我想像这样将字典转换成数据框。一个包含字典键和值(每个值的输入)的数据框成数据框的行。

Start       Beta_value       Cancer            Stage             Chromosome
28878779  0.63078648931418   BRCA  Primary Blood Derived Cancer    chr16
28878779  0.913319324289701  BRCA  Primary Blood Derived Cancer    chr16
.
.
116276795 0.029533524931350  BRCA  Primary Blood Derived Cancer    chr12
116276795 0.0225709542480921 BRCA  Primary Blood Derived Cancer    chr12
.
.

I tried this.. 我尝试过这个

dlist = [[key,value[i][0],value[i][1],value[i][2],value[i][3]]
for key,value in dict1.items()
for i in value]


beta = pd.DataFrame(d, columns = 
['Start','Beta_value','Cancer','Stage','Chromosome'])

It is showing some type error: 它显示了一些类型错误:

   TypeError: list indices must be integers or slices, not list

what am I supposed to do? 我应该做些什么?

Variable i return lists, so need indexing them: 变量i返回列表,因此需要索引它们:

dlist = [[key,i[0],i[1],i[2],i[3]] for key,value in dict1.items() for i in value]

Or add key to list: 或将密钥添加到列表:

dlist = [[key] + i for key,value in dict1.items() for i in value] 
#alternative 
#dlist = [(key, *i) for key,value in dict1.items() for i in value]    

beta = pd.DataFrame(dlist, columns=['Start','Beta_value','Cancer','Stage','Chromosome'])
print (beta)
        Start  Beta_value Cancer                         Stage Chromosome
0    28878779    0.630786   BRCA  Primary Blood Derived Cancer      chr16
1    28878779    0.913319   BRCA  Primary Blood Derived Cancer      chr16
2    28878779    0.429191   BRCA  Primary Blood Derived Cancer      chr16
3    28878779    0.757150   BRCA  Primary Blood Derived Cancer      chr16
4    28878779    0.200534   BRCA  Primary Blood Derived Cancer      chr16
5    28878779    0.472227   BRCA  Primary Blood Derived Cancer      chr16
6    28878779    0.542198   BRCA  Primary Blood Derived Cancer      chr16
7    28878779    0.517081   BRCA  Primary Blood Derived Cancer      chr16
8    28878779    0.354579   BRCA  Primary Blood Derived Cancer      chr16
9    28878779    0.479331   BRCA  Primary Blood Derived Cancer      chr16
10  116276795    0.029534   BRCA  Primary Blood Derived Cancer      chr12
11  116276795    0.022571   BRCA  Primary Blood Derived Cancer      chr12
12  116276795    0.023093   BRCA  Primary Blood Derived Cancer      chr12
13  116276795    0.022679   BRCA  Primary Blood Derived Cancer      chr12
14  116276795    0.046524   BRCA  Primary Blood Derived Cancer      chr12
15  116276795    0.030853   BRCA  Primary Blood Derived Cancer      chr12
16  116276795    0.028026   BRCA  Primary Blood Derived Cancer      chr12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将值是不同长度列表的字典转换为数据框 - Convert a dictionary which values are different-length lists into a dataframe 如何将Python数据框转换为列表列表? - How to convert a Python Dataframe to List of Lists? 如何将列表列表转换为python中的特定数据框 - how to convert lists of list to particular dataframe in python 如何在python中将列表列表转换为数据帧 - How to convert a list of lists into a dataframe in python 如何在python中将列表列表转换为Pandas数据框 - How to convert list of lists into a Pandas dataframe in python Python Pandas:从具有列表列表值的字典创建 DataFrame - Python Pandas: Create DataFrame from dictionary that has values of list of lists 如何将列表列表转换为键为整数且值为 integer 所属的子列表的索引的字典? - How can I convert a list of lists to a dictionary whose keys are integers and values are the index of the sublist to which the integer belongs? 如何将具有列表值的字典转换为显示字典值计数的数据框 - How to convert a dictionary with list values into dataframe showing the counts of values of dictionary 如何访问字典列表中的值,该列表是 oneliner 中 dataframe 中的一列 - How to access values from lists of dictionary which is a column in dataframe in oneliner 如何在 python 中将列表字典转换为 pandas dataframe - how to convert dictionary of list to pandas dataframe in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM