``````

In :

from pandas import *

``````
``````

In :

``````
``````

In :

df

``````
``````

Out:

GDP
USRECQ

DATE

1947-01-01
NaN
0

1947-04-01
NaN
0

1947-07-01
NaN
0

1947-10-01
NaN
0

1948-01-01
0.095023
0

1948-04-01
0.107998
0

1948-07-01
0.117553
0

1948-10-01
0.078371
0

1949-01-01
0.034560
1

1949-04-01
-0.004397
1

``````

I would like to count the number of observation for which USRECQ[DATE+1]==1 and GDP[DATE]>a if GDP[DATE]!='NAN'.

``````

In :

df[np.logical_and(df.shift(-1)['USRECQ'] == 1,df.GDP > 0)]

``````
``````

Out:

GDP
USRECQ

DATE

1948-10-01
0.078371
0

1949-01-01
0.034560
1

``````
``````

In :

df[np.logical_and(df.shift(-1)['USRECQ'] == 1,df.GDP > 0.07)]

``````
``````

Out:

GDP
USRECQ

DATE

1948-10-01
0.078371
0

``````
``````

In :

df[np.logical_and(df.shift(-1)['USRECQ'] == 1,df.GDP < 0.04)]

``````
``````

Out:

GDP
USRECQ

DATE

1949-01-01
0.03456
1

``````

So we can take the `len` of any of these expressions to get a count

``````

In :

len(df[np.logical_and(df.shift(-1)['USRECQ'] == 1,df.GDP < 0.04)])

``````
``````

Out:

1

``````
``````

In :

len(df[np.logical_and(df.shift(-1)['USRECQ'] == 1,df.GDP > 0)])

``````
``````

Out:

2

``````
``````

In :

def count_by_gdp(df,gdp):
''' Count entries where the next entry has USRECQ == 1 and the entry's GDP
is > gdp
'''
return len(df[np.logical_and(df.shift(-1)['USRECQ'] == 1,df.GDP > gdp)])

``````
``````

In :

count_by_gdp(df,0.03)

``````
``````

Out:

2

``````
``````

In :

count_by_gdp(df,0.07)

``````
``````

Out:

1

``````
``````

In :

count_by_gdp(df,-0.01)

``````
``````

Out:

2

``````
``````

In [ ]:

``````