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)
Pandas DataFrame iterrows() MethodThe iterrows() method generates an iterator object of the DataFrame, allowing us to iterate each row in the DataFrame. Each iteration produces an index object and a row object (a Pandas Series object).
You can use the loc and iloc functions to access columns in a Pandas DataFrame. Let's see how. If we wanted to access a certain column in our DataFrame, for example the Grades column, we could simply use the loc function and specify the name of the column in order to retrieve it.
Iterate Over DataFrame Columns One simple way to iterate over columns of pandas DataFrame is by using for loop. You can use column-labels to run the for loop over the pandas DataFrame using the get item syntax ([]) . Yields below output. The values() function is used to extract the object elements as a list.
iterrows() is used to iterate over a pandas Data frame rows in the form of (index, series) pair. This function iterates over the data frame column, it will return a tuple with the column name and content in form of series.
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']
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With