How can I iterate through two Pandas columns?
Question:
Consider:
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
Answers:
Try,
for i in test.index : print test['a'][i], test['b'][i]
to give you,
0 4
1 5
2 6
3 7
Use the DataFrame.itertuples() method:
for a, b in test.itertuples(index=False):
print a, b
You can also use the .iterrows() method. It returns the Index
and Series
per row:
test = DataFrame({'a':range(4),'b':range(4,8)})
for idx, series in test.iterrows():
print series['a'], series['b']
You can use zip
(this is native in Python 3 and can be imported from itertools
as izip
in Python 2.7):
Python 3
for a,b in zip(test.a, test.b):
print(a,b)
Python 2
for a,b in izip(test.a, test.b):
print a,b
Consider:
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
Try,
for i in test.index : print test['a'][i], test['b'][i]
to give you,
0 4
1 5
2 6
3 7
Use the DataFrame.itertuples() method:
for a, b in test.itertuples(index=False):
print a, b
You can also use the .iterrows() method. It returns the Index
and Series
per row:
test = DataFrame({'a':range(4),'b':range(4,8)})
for idx, series in test.iterrows():
print series['a'], series['b']
You can use zip
(this is native in Python 3 and can be imported from itertools
as izip
in Python 2.7):
Python 3
for a,b in zip(test.a, test.b):
print(a,b)
Python 2
for a,b in izip(test.a, test.b):
print a,b