'how to access a specific result of a groupby grouper method grouped with frequency on quarters

df = pd.DataFrame(np.random.choice(pd.date_range('2019-10-01', '2022-10-31'), 15),
                  columns=['Date'])
df['NUM'] = np.random.randint(1, 600, df.shape[0])
df.groupby(pd.Grouper(key='Date', axis=0, freq='Q-DEC')).sum()
df_test = df.groupby(pd.Grouper(key='Date', axis=0, freq='Q-DEC')).sum()
df_test.iloc[-1]

So I can assign the groupby to a variable making another dataframe and then access any one entry of the groupby...in this instance the last entry. My question is can I avoid creating df_test altogether to access the last entry (or any entry I care to access)?



Solution 1:[1]

You can add .tail(1) at the end of groupby statement like this:

df.groupby(pd.Grouper(key='Date', axis=0, freq='Q-DEC')).sum().tail(1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Phoenix