简体   繁体   English

用多个长度和长度不同的值(列表)的字典从字典中创建一个熊猫DataFrame

[英]Creating a panda DataFrame from dictionary with multiple keys and value (list) of different lengths

I have a dictionary of with multiple keys with values(list) that do not have the same length. 我有一个字典,其中包含多个键(其值(列表)的长度不同)。 I would like to read them into a pandas DataFrame.I would like the keys to be the column names and the values to be my rows. 我想将它们读入pandas DataFrame中,我希望键是列名,值是我的行。 Assuming that I have a dictionary with multiple keys, I tried: 假设我有一本具有多个键的字典,我尝试了:

dict
df=pd.from_dict(dict,orient="columns")

But it does not still work. 但是它仍然无法正常工作。 What alternative can I have? 我可以有什么选择?

Use : 采用 :

import pandas as pd 
dataframe1 = pd.DataFrame(dict([(k,pd.Series(v)) for k,v in my_dict.iteritems()]))  

where my_dict is your current dictionary. 其中my_dict是您当前的字典。

Not exactly sure what you want and I assume that you're getting the ValueError: arrays must all be same length error. 不确定您想要什么,并且我假设您正在得到ValueError: arrays must all be same length错误。 A crude work around is simply backfill each list so that all of each list are the same length, then simply pass it to the DataFrame constructor. 粗略的解决方法是简单地回填每个列表,以使每个列表的长度都相同,然后将其传递给DataFrame构造函数。 See example below: 请参见下面的示例:

In [1]: import pandas as pd

In [2]: import numpy as np

In [3]: mydata = {'dict_{:02d}'.format(i): range(1, i+1) for i in range(1, 5)}

In [4]: mydata
Out[4]:
{'dict_01': [1],
 'dict_02': [1, 2],
 'dict_03': [1, 2, 3],
 'dict_04': [1, 2, 3, 4]}

In [5]: max_len = max([len(x) for x in mydata.values()])

In [6]: max_len
Out[6]: 4

In [7]: df = pd.DataFrame({key: vals + [np.nan]*(max_len - len(vals)) for key, vals in mydata.iteritems()})

In [8]: df
Out[8]:
   dict_01  dict_02  dict_03  dict_04
0        1        1        1        1
1      NaN        2        2        2
2      NaN      NaN        3        3
3      NaN      NaN      NaN        4

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

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