Jul-19-2019, 12:48 PM
(This post was last modified: Jul-19-2019, 12:48 PM by Progressive. Edited 2 times in total.)

Hi,

I would like to extract the first 50 data points of each group factor in a data frame.

So far, I stumbled over:

Can someone please shed some light on me?

Thank you!

I got it. You have to use ".iloc" instead of ".loc"

I would like to extract the first 50 data points of each group factor in a data frame.

So far, I stumbled over:

grouped = df.groupby('factor').first()which extracts the first data point (also when it is not a time format as stated in the documentation)

grouped = df.groupby('factor').nth()which extracts the nth data point, so a single one instead of a list

grouped = df.groupby('factor').apply(lambda x: x.iloc[0:2]))which extracts the first 50 rows indeed - but only for the first group instead of for all groups..

Can someone please shed some light on me?

Thank you!

I got it. You have to use ".iloc" instead of ".loc"

grouped = df.groupby('factor').apply(lambda x: x.iloc[0:50])