简体   繁体   English

从系列可能的故障字典中加载熊猫DataFrame吗?

[英]Loading pandas DataFrame from dict of series possible glitch?

I'm constructing a dictionary using a dictionary comprehension which has read_csv embedded within it. 我正在使用嵌入了read_csv的字典理解来构建字典。 This constructs the dictionary fine, but when I then push it into a DataFrame all of my data goes to null and the dates get very wacky as well. 这可以很好地构造字典,但是当我将其推入DataFrame时,我所有的数据都为空,并且日期也变得非常古怪。 Here's sample code and output: 这是示例代码和输出:

In [129]: a= {x.split(".")[0] : read_csv(x, parse_dates=True, index_col=[0])["Settle"] for x in t[:2]}

In [130]: a
Out[130]: 
{'SPH2010': Date
2010-03-19    1172.95
2010-03-18    1166.10
2010-03-17    1165.70
2010-03-16    1159.50
2010-03-15    1150.30
2010-03-12    1151.30
2010-03-11    1150.60
2010-03-10    1145.70
2010-03-09    1140.50
2010-03-08    1137.10
2010-03-05    1136.50
2010-03-04    1122.30
2010-03-03    1118.60
2010-03-02    1117.40
2010-03-01    1114.60
...
2008-04-10    1370.4
2008-04-09    1367.7
2008-04-08    1378.7
2008-04-07    1378.4
2008-04-04    1377.8
2008-04-03    1379.9
2008-04-02    1377.7
2008-04-01    1376.6
2008-03-31    1329.1
2008-03-28    1324.0
2008-03-27    1334.7
2008-03-26    1340.7
2008-03-25    1357.0
2008-03-24    1357.3
2008-03-20    1329.8
Name: Settle, Length: 495,
 'SPM2011': Date
2011-06-17    1279.4
2011-06-16    1269.0
2011-06-15    1265.4
2011-06-14    1289.9
2011-06-13    1271.6
2011-06-10    1269.2
2011-06-09    1287.4
2011-06-08    1277.0
2011-06-07    1284.8
2011-06-06    1285.0
2011-06-03    1296.3
2011-06-02    1312.4
2011-06-01    1312.1
2011-05-31    1343.9
2011-05-27    1329.9
...
2009-07-10    856.6
2009-07-09    861.2
2009-07-08    856.0
2009-07-07    861.7
2009-07-06    877.9
2009-07-02    875.8
2009-07-01    902.6
2009-06-30    900.3
2009-06-29    908.0
2009-06-26    901.1
2009-06-25    903.8
2009-06-24    885.2
2009-06-23    877.6
2009-06-22    876.0
2009-06-19    903.4
Name: Settle, Length: 497}

In [131]: DataFrame(a)
Out[131]: 
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 806 entries, 2189-09-10 03:33:28.879144 to 1924-01-20 06:06:06.621835
Data columns:
SPH2010    0  non-null values
SPM2011    0  non-null values
dtypes: float64(2)

Thanks! 谢谢!

EDIT: 编辑:

I've also tried doing this with concat and I get the same results. 我也尝试过用concat做到这一点,并且得到了相同的结果。

You should be able to use concat and unstack . 您应该能够使用concatunstack Here's an example: 这是一个例子:

df1 = pd.Series([1, 2], name='a')
df2 = pd.Series([3, 4], index=[1, 2], name='b')
d = {'A': s1, 'B': s2} # a dict of Series

In [4]: pd.concat(d)
Out[4]: 
A  0    1
   1    2
B  1    3
   2    4

In [5]: pd.concat(d).unstack().T
Out[5]: 
    A   B
0   1 NaN
1   2   3
2 NaN   4

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

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