Pandas: How could I iterate two dataframes which have exactly same format?
You could use itertools.izip
:
for ( idxRow, s1 ), ( _, s2 ) in itertools.izip( df0.iterrows(), df1.iterrows() ) :
for ( idxCol, v1 ), ( _, v2 ) in itertools.izip( s1.iteritems(), s2.iteritems() ) :
print ( v1, v2, idxCol, idxRow )
In:
X Y Z
a 1.171124 0.853229 1.416635
b 0.971665 -1.727410 -0.055180
Out:
(1.1711241491561419, 1.3715317727366974, 'X', 'a')
(0.85322862359611618, 0.72799908412372294, 'Y', 'a')
(1.4166350896829785, 2.0068549773211006, 'Z', 'a')
(0.9716653056530119, 0.94413346620976102, 'X', 'b')
(-1.727409829928936, 2.9839447205351157, 'Y', 'b')
(-0.055180403519242693, 0.0030448769325464513, 'Z', 'b')
An update to @usual me's answer for python 3. Now the standard library's zip
can handle this for us:
for (idxRow, s1), (_, s2) in zip(df0.iterrows(), df1.iterrows()):
for (idxCol, v1), (_, v2) in zip(s1.iteritems(), s2.iteritems()):
print (v1, v2, idxCol, idxRow)