1

我正在使用嵌入了 read_csv 的字典理解来构建字典。这可以很好地构建字典,但是当我将其推送到 DataFrame 中时,我的所有数据都变为 null 并且日期也变得非常古怪。这是示例代码和输出:

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)

谢谢!

编辑:

我也试过用 concat 做这个,我得到了相同的结果。

4

1 回答 1

0

您应该能够使用concatunstack。这是一个例子:

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
于 2013-01-10T00:14:22.873 回答