Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
slice per group
#1
Hi,

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])
Reply


Messages In This Thread
slice per group - by Progressive - Jul-19-2019, 12:48 PM
RE: slice per group - by scidam - Jul-19-2019, 01:07 PM
RE: slice per group - by Progressive - Jul-19-2019, 01:50 PM
RE: slice per group - by scidam - Jul-20-2019, 06:52 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Inserting slice of array objects into different slice ThemePark 4 2,535 Apr-01-2020, 01:10 PM
Last Post: ThemePark
  TypeError: '(slice(None, None, None), 0)' is an invalid key zaki424160 1 15,199 Jul-17-2019, 11:53 PM
Last Post: scidam
  Melt or Slice Grin 0 2,178 Jun-24-2018, 06:02 PM
Last Post: Grin
  How to group variables & check correlation of group variables wrt single variable SriRajesh 2 2,960 May-23-2018, 03:01 PM
Last Post: SriRajesh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020