0

I am looking to get records of location (first 2) out of a dataframe where column ID values are constant and then next 2 location records when column value is changed.

ID  Location
51699457    LOC000009723262
51699457    LOC000009844932
51699457    LOC000011336522
51699457    LOC000018023392
51699458    LOC000008089735
51699458    LOC000016725720
51699458    LOC000028801236
51699459    LOC000034119383
51699459    LOC000034901728
51699459    LOC000035640991
51699459    LOC000035658463

O/P

ID  Location
51699457    LOC000009723262
51699457    LOC000009844932
51699458    LOC000008089735
51699458    LOC000016725720
51699459    LOC000035640991
51699459    LOC000035658463

any help would be appreciated please.

Shahrukh Khalidi
  • 105
  • 1
  • 1
  • 9

1 Answers1

0

This worked.

df.groupby('ID').head(2).reset_index(drop=True)
Pang
  • 9,564
  • 146
  • 81
  • 122
Shahrukh Khalidi
  • 105
  • 1
  • 1
  • 9