使用iterrows在3列中循环使用df,但得到的错误表明只有2列

时间:2017-05-24 14:21:19

标签: python pandas for-loop iterator

我的df结构如下:

                T1                  T2                 T3
14577 2016-01-01 06:55:18 2016-01-01 06:58:00 2016-01-01 07:03:58
14578 2016-01-01 08:25:29 2016-01-01 08:29:02 2016-01-01 08:50:23
14579 2016-01-01 10:38:55 2016-01-01 10:40:00 2016-01-01 11:06:43
14580 2016-01-01 13:38:05 2016-01-01 13:45:23 2016-01-01 13:51:47
...

然后我尝试迭代遍历每一行:

for i,j,k in df.iterrows():
    print(i,j,k)

但是我收到了这个错误:

File "<ipython-input-66-b6aadd823294>", line 1, in <module>
for i,j,k in df.iterrows():
ValueError: not enough values to unpack (expected 3, got 2)

为了清楚起见,这里是df的维度:

>>>df.shape
Out[68]: 
(434, 3)

1 个答案:

答案 0 :(得分:1)

df.iterrows()将迭代DataFrame行作为(index,Series)对(source)。该系列将包含整行。

您可以尝试这样来打印值

for row, series in df.iterrows():
    print(series['T1'], series['T2'],series['T3'])