Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python and pandas - how to access a column using iterrows

Tags:

python

pandas

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) 
like image 874
Tampa Avatar asked Apr 18 '14 01:04

Tampa


People also ask

How do you use Iterrows in pandas?

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).

How do you access a specific column in Python?

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.

How do you go through a column in pandas?

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.

What does Iterrows return pandas?

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.


2 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'] 
like image 139
Marius Avatar answered Sep 21 '22 13:09

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)        
like image 41
Acorbe Avatar answered Sep 22 '22 13:09

Acorbe