bigballerbrand
bigballerbrand

Reputation: 51

Python Pandas: Indexing 2 Rows Before Meeting Criteria

I have the following Dataframe below:

Rec  Channel  Value1  Value2 
Pre             10      20
Pre             35      42
Event    A      23      39
FF              50      75
Post     A      79      11
Post     B      88      69

With the following code:

idxall = df[df['Chan'].isin({'A', 'B'})]
for i in range(len(idxall)):
print(idxall.iloc[[i]]) 

This is currently giving me the output of:

Rec  Channel  Value1  Value2 
Event    A      23      39
Post     A      79      11
Post     B      88      69

However, I would like the output to pull all instances 2 rows above every time the Channel column detects an 'A' or 'B'.

Desired Output:

Rec  Channel  Value1  Value2
Pre             10      20
Event    A      23      39
FF              50      75

Can someone please advise accordingly?

Upvotes: 0

Views: 53

Answers (1)

sacuL
sacuL

Reputation: 51395

You can try something like this:

idx = df.loc[df.Channel.isin(['A', 'B'])].index

new_df = df.iloc[idx - 2]

>>> new_df
     Rec Channel  Value1  Value2
0    Pre              10      20
2  Event       A      23      39
3     FF              50      75

Explanation: idx in the code above is the indices where you Channel column is A or B. df.iloc[idx - 2] simply selects the rows that are at found 2 rows above those indices.

Note that this works as long as your index of your df is a consecutive count, i.e. that df.index returns something like: RangeIndex(start=0, stop=6, step=1). If that is not the case, start by resetting the index to satisfy that condition, using df.reset_index(drop=True, inplace = True)

EDIT: Based on your comment, it seems like you are looking to only print the rows, rather than create a new dataframe based on the rows. In that case, you can use your basic loop format:

idx = df.loc[df.Channel.isin(['A', 'B'])].index

for i in idx:
    print(df.iloc[[i - 2]])

Upvotes: 1

Related Questions