In [35]: test = pd.DataFrame({'a':range(4),'b':range(4,8)})
In [36]: test
Out[36]:
a b
0 0 4
1 1 5
2 2 6
3 3 7
In [37]: for i in test['a']:
....: print i
....:
0
1
2
3
In [38]: for i,j in test:
....: print i,j
....:
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
ValueError: need more than 1 value to unpack
In [39]: for i,j in test[['a','b']]:
....: print i,j
....:
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
ValueError: need more than 1 value to unpack
In [40]: for i,j in [test['a'],test['b']]:
....: print i,j
....:
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
ValueError: too many values to unpack
问问题
62385 次
4 回答
43
使用DataFrame.itertuples()
方法:
for a, b in test.itertuples(index=False):
print a, b
于 2013-02-28T00:56:25.650 回答
23
您可以使用zip
(这在 python 3 中是本机的,可以从python 2.7 中导入)itertools
:izip
蟒蛇 3
for a,b in zip(test.a, test.b):
print(a,b)
蟒蛇 2
for a,b in izip(test.a, test.b):
print a,b
于 2016-09-09T20:47:55.753 回答
6
尝试,
for i in test.index : print test['a'][i], test['b'][i]
为你带来,
0 4
1 5
2 6
3 7
于 2013-02-28T00:45:11.420 回答
5
我仍在尝试自己学习熊猫。
您也可以使用该.iterrows()
方法,它返回Index
和Series
每行:
test = DataFrame({'a':range(4),'b':range(4,8)})
for idx, series in test.iterrows():
print series['a'], series['b']
于 2013-02-28T00:57:39.527 回答