-2

I am trying to extract data from Quandl and I want to get the Date and 'Open' value (respectively) for each row. However, I am not sure what I should. Been trying different method that hasn't worked out. Below is an example:

data = quandl.get("EOD/PG", trim_start = "2011-12-12", trim_end =
"2011-12-30", authtoken=quandl.ApiConfig.api_key)

data = data.reset_index()
sta = data[['Date','Open']]

for row in sta:
     price = row.iloc[:,1]
     date = row.iloc[:, 0]
MathiasRa
  • 825
  • 2
  • 12
  • 24
  • Possible duplicate of [How to iterate over rows in a DataFrame in Pandas?](https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas) – Sosel Oct 18 '17 at 07:33

1 Answers1

1

What you're doing with the code you have provided is iterating through the column names, i.e. you get 'Date' on the first iteration, and 'Open' on the next (and last).

To iterate through a dataframe by row, you can use any one the .iterrows(), .iteritems() or .itertuples() methods.

For example,

for row in data.itertuples():
    price = row.Open
    date = row.Date

Having said so, iterating through a pandas dataframe is really slow. Chances are, whatever you intend to do could be done faster by making use of pandas' vectorization, i.e. without a loop.

Ken Wei
  • 3,020
  • 1
  • 10
  • 30
  • Thanks. I look at vectorization later. However, if I can ask one follow-up question: How do I (only) get the price value without the index number. – MathiasRa Oct 18 '17 at 08:13