Home > Software design >  Drop rows before the last True False
Drop rows before the last True False

Time:09-13

I have a df like this and I want to drop everything before the last row 'isBuyer' == True and prev_isBuyer == False

The row 4 is the last row that contain isBuyer == True and prev_isBuyer == False, I want to drop all the rows before that. You can see my expected result. Not sure if i'm clear enough.

Some information, the last row of my df 'isBuyer' will always be False.

      symbol  isBuyer prev_isBuyer
0   DOCKUSDT     True          NaN
1   DOCKUSDT    False         True
2   DOCKUSDT     True        False
3   DOCKUSDT    False         True
4   DOCKUSDT     True        False
5   DOCKUSDT     True         True
6   DOCKUSDT     True         True
7   DOCKUSDT    False         True
8   DOCKUSDT    False        False
9   DOCKUSDT    False        False
10  DOCKUSDT    False        False
11  DOCKUSDT    False        False
12  DOCKUSDT    False        False
13  DOCKUSDT    False        False
14  DOCKUSDT    False        False
15  DOCKUSDT    False        False

expected result:

      symbol  isBuyer prev_isBuyer
4   DOCKUSDT     True        False
5   DOCKUSDT     True         True
6   DOCKUSDT     True         True
7   DOCKUSDT    False         True
8   DOCKUSDT    False        False
9   DOCKUSDT    False        False
10  DOCKUSDT    False        False
11  DOCKUSDT    False        False
12  DOCKUSDT    False        False
13  DOCKUSDT    False        False
14  DOCKUSDT    False        False
15  DOCKUSDT    False        False

CodePudding user response:

You can use boolean indexing with a custom reversed cummin:

df[df.loc[::-1, 'prev_isBuyer'].diff().shift().ne(-1).cummin()]

How it works:

  • reverse Series
  • get diff and shift to identify the last False/True
  • cummin to get rid of all previous True (as the Series is inverted)

output:

      symbol  isBuyer prev_isBuyer
4   DOCKUSDT     True        False
5   DOCKUSDT     True         True
6   DOCKUSDT     True         True
7   DOCKUSDT    False         True
8   DOCKUSDT    False        False
9   DOCKUSDT    False        False
10  DOCKUSDT    False        False
11  DOCKUSDT    False        False
12  DOCKUSDT    False        False
13  DOCKUSDT    False        False
14  DOCKUSDT    False        False
15  DOCKUSDT    False        False

CodePudding user response:

Simple solution.

# considering df is your dataframe containing columns listed
# find elements that match your condition
separator_elements = df[(df['prev_isBuyer'] == False) & (df['isByuer'] == True)]
# get index of first one
index = separator_elements.index[0]
# splitting the dataframe by index
df_filtered = df.iloc[index:, :] 
  • Related