I wish to get the last group of my group by:
JavaScript
x
2
1
df.groupby(pd.TimeGrouper(freq='M')).groups[-1]:
2
but that gives the error:
KeyError: -1
Using get_group
is useless as I don’t know the last group’s value (unless there’s a specific way to get that value?). Also I might want to get the last 2 groups, etc
How do I do this?
Advertisement
Answer
You can call last
which computes the last values for each group and use iloc
to get the row values and access the index group values using the name
attribute, there is probably a better way but unable to figure this out yet:
JavaScript
1
39
39
1
In [170]:
2
# dummy data
3
df = pd.DataFrame({'a':['1','2','2','4','5','2'], 'b':np.random.randn(6)})
4
df
5
Out[170]:
6
a b
7
0 1 0.097176
8
1 2 -1.400536
9
2 2 0.352093
10
3 4 -0.696436
11
4 5 -0.308680
12
5 2 -0.217767
13
In [179]:
14
15
gp = df.groupby('a', sort=False)
16
gp.get_group(df.groupby('a').last().iloc[-1].name)
17
Out[179]:
18
a b
19
4 5 0.608724
20
In [180]:
21
22
df.groupby('a').last().iloc[-2:]
23
Out[180]:
24
b
25
a
26
4 0.390451
27
5 0.608724
28
In [181]:
29
30
mult_groups = gp.last().iloc[-2:].index
31
In [182]:
32
33
for gp_val in mult_groups:
34
print(gp.get_group(gp_val))
35
a b
36
3 4 0.390451
37
a b
38
4 5 0.608724
39