[英]Creating a dataframe from a dictionary of different lengths
With the following dictionary: 使用以下字典:
{'A': [DatetimeIndex([], dtype='datetime64[ns]', name=u'Timestamp', freq=None)],
'B': [DatetimeIndex(['2010-04-15 16:19:00', '2010-04-15 16:20:00',
'2010-04-15 16:23:00'],
dtype='datetime64[ns]', name=u'Timestamp', length=6, freq=None)]}
I want to create the following dataframe: 我要创建以下数据框:
A B
NaN 2010-04-15 16:19:00
NaN 2010-04-15 16:20:00
NaN 2010-04-15 16:23:00
A and B have different DatetimeIndex lengths so I want to fill the shorter one (in this case column A) with NaN's. A和B具有不同的DatetimeIndex长度,因此我想用NaN填充较短的(在本例中为A列)。
Thanks for your help :) 谢谢你的帮助 :)
If you turn your indices into Series
objects, the standard DataFrame constructor can do exactly what you want: 如果将索引转换为Series
对象,则标准DataFrame构造函数可以完全满足您的要求:
>>> data = {'A': [pd.DatetimeIndex([])],
... 'B': [pd.DatetimeIndex(['2010-04-15 16:19:00',
'2010-04-15 16:20:00',
'2010-04-15 16:23:00'])]}
>>> pd.DataFrame({key: pd.Series(val[0], index=val[0])
for key, val in data.items()})
A B
2010-04-15 16:19:00 NaT 2010-04-15 16:19:00
2010-04-15 16:20:00 NaT 2010-04-15 16:20:00
2010-04-15 16:23:00 NaT 2010-04-15 16:23:00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.