通过具有循环的列将两个Pandas系列附加到数据框

时间:2017-06-03 03:17:24

标签: python pandas

我有一个数据帧和两个Pandas系列ac和cc,我想将这两个系列作为带有循环的列附加。但问题是我的数据帧有时间索引,系列为整数

A='a'

cc  = pd.Series(np.zeros(len(A)*20))
ac  = pd.Series(np.random.randn(10))

index = pd.date_range(start=pd.datetime(2017, 1,1), end=pd.datetime(2017, 1, 2), freq='1h')

df = pd.DataFrame(index=index)

我已经回答了我的问题,但没有循环here

现在,我需要添加一个循环,但我在键中出错:

az = [cc, ac]

for i in az:
    df.join(
            pd.concat(
            [pd.Series(s.values, index[:len(s)]) for s in [i]],
            axis=1, keys=[i]
           )
         )

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), ,a.any() or a.all().

我尝试使用keys = [i.all ()],我有正确的答案,除了我没有列名称,我有真假。

最终结果应该是这样的:

                     cc    ac   
2017-01-01 00:00:00   1    0.247043 
2017-01-01 01:00:00   1    -0.324868 
2017-01-01 02:00:00   1    -0.004868
2017-01-01 03:00:00   1    0.047043 
2017-01-01 04:00:00   1    -0.447043 
2017-01-01 05:00:00 NaN    NaN 
...                 ...    ...

1 个答案:

答案 0 :(得分:1)

创建一个元组列表,其中第一个元素是列名,第二个元素是系列本身。

az = [('cc', cc), ('ac', ac)]

for c, s in az:
    df[c] = pd.Series(s.values, index[:len(s)])

                      cc        ac
2017-01-01 00:00:00  0.0  2.062265
2017-01-01 01:00:00  0.0 -0.225066
2017-01-01 02:00:00  0.0 -1.698330
2017-01-01 03:00:00  0.0 -1.068081
2017-01-01 04:00:00  0.0  0.142956
2017-01-01 05:00:00  0.0 -1.244232
2017-01-01 06:00:00  0.0 -1.072311
2017-01-01 07:00:00  0.0  0.242069
2017-01-01 08:00:00  0.0  0.120093
2017-01-01 09:00:00  0.0 -0.335500
2017-01-01 10:00:00  0.0       NaN
2017-01-01 11:00:00  0.0       NaN
2017-01-01 12:00:00  0.0       NaN
2017-01-01 13:00:00  0.0       NaN
2017-01-01 14:00:00  0.0       NaN
2017-01-01 15:00:00  0.0       NaN
2017-01-01 16:00:00  0.0       NaN
2017-01-01 17:00:00  0.0       NaN
2017-01-01 18:00:00  0.0       NaN
2017-01-01 19:00:00  0.0       NaN
2017-01-01 20:00:00  NaN       NaN
2017-01-01 21:00:00  NaN       NaN
2017-01-01 22:00:00  NaN       NaN
2017-01-01 23:00:00  NaN       NaN
2017-01-02 00:00:00  NaN       NaN