DooDoo
DooDoo

Reputation: 13447

How to get next rows after filtered rows in Pandas

I have a Data Frame called Data. I wrote this filter for filter the rows:

data[data["Grow"] >= 1.5]

It returned some rows like these:

                   PriceYesterday       Open        High        Low 
    ------------------------------------------------------------------
          7            6888.0          6881.66     7232.0       6882.0
          53           7505.0          7555.72     7735.0       7452.0
          55           7932.0          8093.08     8120.0       7974.0  
          64           7794.0          7787.29     8001.0       7719.0
          ...

As you see there are some rows in the indexes 7, 53, 55 ,.... Now I want to get rows in indexes 8, 54, 56, ... too. Is there any straight forward way to do this? Thanks

Upvotes: 1

Views: 1425

Answers (3)

jezrael
jezrael

Reputation: 862551

You can use Index.intersection for avoid error if matching last row and want select not exist index values:

data = pd.DataFrame({
        'A':list('abcdef'),
         'B':[4,5,4,5,5,4],
         'Grow':[0,8,2,0.4,2,3.3],

})

df1 = data[data["Grow"] >= 1.5]
print (df1)
   A  B  Grow
1  b  5   8.0
2  c  4   2.0
4  e  5   2.0
5  f  4   3.3

df2 = data.loc[data.index.intersection(df1.index + 1)]
print (df2)
   A  B  Grow
2  c  4   2.0
3  d  5   0.4
5  f  4   3.3

Another idea is select by shifted values by Series.shift

df1 = data[data["Grow"] >= 1.5]
df2 = data[data["Grow"].shift() >= 1.5]
print (df2)
   A  B  Grow
2  c  4   2.0
3  d  5   0.4
5  f  4   3.3

df1 = data[data["Grow"] >= 1.5]
df2 = data.loc[df1.index + 1]
print (df2)
    

KeyError: "Passing list-likes to .loc or [] with any missing labels is no longer supported. The following labels were missing: Int64Index([6], dtype='int64'). See https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#deprecate-loc-reindex-listlike"

Upvotes: 4

divingTobi
divingTobi

Reputation: 2300

You should create a mask, and then shift that mask by one:

import numpy as np

df = pd.DataFrame({'a': np.random.random(20)})

print(df)
mask = df['a']>0.8
print("items that fit the mask:")
print(df.loc[mask])
print("items following these:")
print(df.loc[mask.shift().fillna(False)])

In your specific case I believe it would be

data.loc[(data["Grow"] >= 1.5).shift().fillna(False)]

Upvotes: 1

CodeNStuff
CodeNStuff

Reputation: 314

data[data.shift()["Grow"] >= 1.5]

The shift moves every cell one step to the end of the frame. So this says: Give my those entries, which predecessors match my criteria.

Upvotes: 0

Related Questions