hernanavella
hernanavella

Reputation: 5552

How to run a conditional query that selects for the first occurrence only in Pandas?

My data is intraday stock prices data, multiple days. This is a simplified version:

            Close   dif                                                 
2015-01-02  2035.25 -1.30                       
2015-01-02  2015.25 -3.18   
2015-01-05  2035.25 -1.35                       
2015-01-05  2015.25 -4.18                   
2015-01-06  1988.00 -2.30                       
2015-01-06  1988.00 -2.32

I'm using a conditional instruction in pandas of the form:

data['Test'] = ""
data.loc[(data['dif'] < 0) & (data['dif'] > -100), 'Test'] = data['Close']

I'm hitting a roadblock because I only care about the first time the condition is met on each day. How can I accomplish this?.

This would be the output:

            Close   dif    Test                                             
2015-01-02  2035.25 -1.30  2035.25                      
2015-01-02  2015.25 -3.18   
2015-01-05  2045.25 -1.35  2045.25                  
2015-01-05  2015.25 -4.18                   
2015-01-06  1989.00 -2.30  1989.00                  
2015-01-06  1988.00 -2.32

Thanks

Upvotes: 2

Views: 84

Answers (1)

Stefan
Stefan

Reputation: 42905

To select the first item of each daily group that meets your condition:

data[(data['dif'] < 0) & (data['dif'] > -100)].groupby('date')['Close', 'dif'].first()

to yield:

          Close   dif
date                 
1/2/15  2035.25 -1.30
1/5/15  2035.25 -1.35
1/6/15  1988.00 -2.30

Upvotes: 2

Related Questions