Let's say I have a DataFrame that looks like this:
Categories Values
0 Category 0 1
1 Category 0 0
2 Category 0 -1
3 Category 0 0
4 Category 1 1
5 Category 1 0
6 Category 1 -1
7 Category 1 0
8 Category 2 1
9 Category 2 0
10 Category 2 -1
11 Category 2 0
12 Category 3 -1
13 Category 3 0
14 Category 3 0
15 Category 3 1
16 Category 4 -1
17 Category 4 0
18 Category 4 0
19 Category 4 1
20 Category 5 -1
21 Category 5 0
22 Category 5 0
23 Category 5 1
I want a time-efficient way to get two things of the last non-zero entries of Values of each group:
(1):the indices,
(2):the entries
The desired output of (1) is: [2,6,10,15,19,23] in the form of pandas Series
The desired output of (2) is: [-1,-1,-1,1,1,1] in the form of pandas Series
Thank you in advance guys
EDIT: added the python code for generating the above DataFrame:
import pandas as pd
n = 4
m = 3
df = pd.DataFrame({'Categories': [f'Category {i//n}' for i in range(2*m*n)],
'Values' : [1,0,-1,0]*m+ [-1,0,0,1]*m})