How to get last 'n' groups after
df.groupby() and combine them as a dataframe.
data = pd.read_sql_query(sql=sqlstr, con=sql_conn, index_col='SampleTime') grouped = data.groupby(data.index.date,sort=False)
grouped.ngroups i am getting total number of groups 277. I want to combine last 12 groups and generate a dataframe.
GroupBy objects are iterables. To extract the last n elements of an iterable, there's generally no need to create a list from the iterable and slice the last n elements. This will be memory-expensive.
from collections import deque from operator import itemgetter grouped = data.groupby(data.index.date, sort=False) res = pd.concat(deque(map(itemgetter(1), grouped), maxlen=12))
As described in the
Once a bounded length
dequeis full, when new items are added, a corresponding number of items are discarded from the opposite end.... They are also useful for tracking transactions and other pools of data where only the most recent activity is of interest.