python and pandas – how to access a column using iterrows

Question:

wowee…..how to use iterrows with python and pandas? If I do a row iteration should I not be able to access a col with row[‘COL_NAME’]?

Here are the col names:

print df
Int64Index: 152 entries, 0 to 151
Data columns:
Date          152  non-null values
Time          152  non-null values
Time Zone     152  non-null values
Currency      152  non-null values
Event         152  non-null values
Importance    152  non-null values
Actual        127  non-null values
Forecast      86  non-null values
Previous      132  non-null values
dtypes: object(9)

for row in df.iterrows():
    print row['Date']

Traceback (most recent call last):
  File "/home/ubuntu/workspace/calandar.py", line 34, in <module>
    print row['Date']
TypeError: tuple indices must be integers, not str

if I print 1 row:

(0, Date                                                 Sun Apr 13
Time                                                      17:30
Time Zone                                                   GMT
Currency                                                    USD
Event         USD Fed's Stein Speaks on Financial Stability ...
Importance                                                  Low
Actual                                                      NaN
Forecast                                                    NaN
Previous                                                    NaN
Name: 0)
Asked By: Tampa

||

Answers:

iterrows gives you (index, row) tuples rather than just the rows, so you should be able to access the columns in basically the same way you were thinking if you just do:

for index, row in df.iterrows():
    print row['Date']
Answered By: Marius

If you want to iterate across your database and apply a function to each row, you might also want to consider the apply function

def print_row(r):
    print r['Date']

df.apply(print_row, axis = 1)       
Answered By: Acorbe
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.