I have a DataFrame
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
Output:
c1 c2
0 10 100
1 11 110
2 12 120
Now I want to iterate over the rows of this frame. For every row I want to be able to access its elements (values in cells) by the name of the columns. For example:
for row in df.rows:
print row['c1'], row['c2']
Is it possible to do that in pandas?
I found this 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():
or
for row in df.iterrows():
But I do not understand what the row
object is and how I can work with it.
To loop all rows in a
dataframe
and use values of each row conveniently,namedtuples
can be converted tondarray
s. For example:Iterating over the rows:
results in:
Please note that if
index=True
, the index is added as the first element of the tuple, which may be undesirable for some applications.Adding to the answers above, sometimes a useful pattern is:
Which results in:
DataFrame.iterrows is a generator which yield both index and row
IMHO, the simplest decision
To loop all rows in a
dataframe
you can use:Why complicate things?
Simple.