0

Can someone help me with a pandas question? I have a timeseries dataframe such as this:

                       GOOG     AAPL
2010-12-09 16:00:00    591.50   551
2010-12-10 16:00:00    592.21   523
2010-12-13 16:00:00    594.62   578
2010-12-14 16:00:00    594.91   567
2010-12-15 16:00:00    590.30   577
...

I need to loop through each timestamp and test whether AAPL is > 570. If it is, then I want to print the date and the price of AAPL for that entry. Is this possible?

1
  • I've only used pandas once in the past, but if I'm recalling correctly it is built on top of numpy so can you use my_dataframe.where(my_dataframe[:,2]>570) Commented Oct 13, 2013 at 22:55

2 Answers 2

3

There's no need for any looping, one of the main benefits of pandas being built on numpy is it can easily operate on whole columns. It's as simple as:

df['AAPL'][df['AAPL'] > 570]

Output:

2010-12-13  16:00:00    578
2010-12-15  16:00:00    577
Name: AAPL, dtype: int64
Sign up to request clarification or add additional context in comments.

3 Comments

this is done more succinctly as df.loc[df['AAPL']>570,'AAPL'] (and if you do assignment, you MUST do it this way
Also, what if only want to output the month-day and price?
@AlexLee: If you're having trouble with that, it might be a good idea to ask a new question along the lines of "Extracting month and day from datetime index", with some easily reproducible data. Stack Overflow works best with self-contained questions that solve a single problem.
1

Ah ha I got it:

What you can do since it is built on top of numpy is this:

my_dataframe[my_dataframe.AAPL > 570]

and you're almost done. From here you have all the rows that correspond to AAPL > 570, now it's just printing out the values you need:

valid_rows = my_dataframe[my_dataframe.AAPL > 570]
for row in valid_rows.to_records():
    print row[1],row[2]

The dataframe.where can be used for searching the entire frame. I had forgotten that pandas made it extremely easy to reference columns.

2 Comments

I'm sure there may be a better way to do this, but this is what I could remember off the top of my head.
Also, I modify Ryan's code a bit for row in my_dataframe[my_dataframe.AAPL > 1].to_records(): print row[0],row[2]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.