Roman Roman - 6 months ago 73
Python Question

How to iterate over rows in a DataFrame in Pandas?

I have a DataFrames from pandas:

import pandas as pd
inp = [{'c1':10, 'c2':100}, {'c1':11,'c2':110}, {'c1':12,'c2':120}]
df = pd.DataFrame(inp)
print df


c1 c2
0 10 100
1 11 110
2 12 120

Now I want to iterate over the rows of the above frame. For every row I want to be able to access its elements (values in cells) by the name of the columns. So, for example, I would like to have something like that:

for row in df.rows:
print row['c1'], row['c2']

Is it possible to do that in pandas?

I found similar question. But it does not give me the answer I need. For example, it is suggested there to use:

for date, row in df.T.iteritems():


for row in df.iterrows():

But I do not understand what the
object is and how I can work with it.


iterrows is a generator which yield both index and row

In [18]: for index, row in df.iterrows():
   ....:     print row['c1'], row['c2']
10 100
11 110
12 120