Back
There is a DataFrame from pandas:
import pandas as pdinp = [{'e2':20, 'e3':200}, {'e2':22,'e3':220}, {'e2':23,'e3':230}]df = pd.DataFrame(inp)print df
The output it showed:
e2 e30 20 2001 22 2202 23 230
After this I want iterate over the rows of this frame. I want to access the elements by the name of the columns. E.x.
for row in df.rows: print row['e2'], row['e3']
Can I do this in Pandas?
You can yield both index and row using DataFrame.iterrows . E.g.for index, row in df.iterrows(): print(row['e2'], row['e3'])Output: 20 200 22 220 23 230
To iterate rows in a DataFrame you can use the following ways-
Using DataFrame.itertuples()
for row in df.itertuples(index=True, name=’Pandas’):
print getattr(row,'e2'), getattr(row,'e3')
31k questions
32.8k answers
501 comments
693 users