简体   繁体   English

熊猫从字典中的矩阵创建数据框

[英]Pandas creating data frame from matrix in dictionary

I have a dictionary Dict1 with keys as Dates and Sims. 我有一个字典Dict1 ,其键为Dates and Sims.

Dates is an array with shape 100x1 and Sims has shape 100x5 Dates是形状为100x1的数组, Sims形状为100x5

I am trying: 我在尝试:

import pandas as pd
df = pd.Dataframe.from_dict(Dict1) 

But errors out due to size of Sims. 但是由于模拟人生的规模而导致错误。 Is there a way I can create the DataFrame with each row of column Sims has size 5? 有没有一种方法可以使Sims列的每一行的大小为5,从而创建DataFrame? ie each row can be stored as list or array of size 5. 也就是说,每一行都可以存储为大小为5的列表或数组。

Edit: 编辑:

Dict1['Dates']
array([datetime.datetime(2016, 11, 1, 0, 0),
       datetime.datetime(2016, 11, 1, 1, 0),
       datetime.datetime(2016, 11, 1, 2, 0), ...,
       datetime.datetime(2025, 12, 31, 21, 0),
       datetime.datetime(2025, 12, 31, 22, 0),
       datetime.datetime(2025, 12, 31, 23, 0)], dtype=object)


Dict1['Sims']

array([[ 63.89694316,  35.8551162 ,  40.36134283, 57.23648392,
         35.96607425,  61.166471  ],
       [ 47.94894386,  53.95396849,  48.94336457, 51.04541849,
         28.69973176,  49.78683505],
       [ 63.90314179,  43.29467789,  36.97811714, 52.33639618,
         45.24190878,  69.9059308 ]...]])

Edit2: 编辑2:

I am looking to create the dataframe such that I can perform the following operation: 我正在寻找创建数据框,以便可以执行以下操作:

print(df[datetime.datetime(2016, 11, 1, 0, 0)])

[ 63.89694316,  35.8551162 ,  40.36134283, 57.23648392,
                 35.96607425,  61.166471  ]

You can use your Dict1['Dates'] as the index. 您可以使用Dict1['Dates']作为索引。

df = pd.DataFrame(Dict1['Sims'], index=Dict1['Dates'])
df.ix[datetime.datetime(2016, 11, 1, 0, 0)]

Note that you should use the df.ix[key] indexer, since df[key] defaults to looking up a column, not a row. 请注意,您应该使用df.ix[key]索引器,因为df[key]默认情况下是查找列而不是行。


Alternatively, if you really want a single column containing list s, make sure that Dict1['Sims'] is a Python list, not a Numpy array before creating your data frame. 另外,如果您确实希望包含list的单个列,请在创建数据框之前确保Dict1['Sims']是Python列表,而不是Numpy数组。

df = pd.DataFrame({'Sims': Dict1['Sims'].tolist()}, index=Dict1['Dates'])

The {'Sims': ...} construct tricks Pandas into interpreting the data as a single series of list s, rather than a multi-dimensional array. {'Sims': ...}构造欺骗Pandas将数据解释为单个list ,而不是多维数组。

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

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