3

I have a dataframe like this :

   id  values
0   1       3
1   1       6
2   1       3
3   2       7
4   2       6
5   2       3
6   2       9

And I want to delete the first line of each group based on id,the result should like this:

   id  values
1   1       6
2   1       3
4   2       6
5   2       3
6   2       9

I tried it done by: df = df.groupby('id').agg(lambda x:x[1:]),but it doesn't work.

Can someone help me?Thanks in advance

giser_yugang
  • 6,058
  • 4
  • 21
  • 44
  • 1
    Does this answer your question? [Python: Pandas - Delete the first row by group](https://stackoverflow.com/questions/31226142/python-pandas-delete-the-first-row-by-group) – RichieV Sep 03 '20 at 04:10

2 Answers2

5

Use apply with iloc:

df = df.groupby('id', group_keys=False).apply(lambda x:x.iloc[1:])
#also working, not sure if generally
#df = df.groupby('id', group_keys=False).apply(lambda x:x[1:])
print (df)
   id  values
1   1       6
2   1       3
4   2       6
5   2       3
6   2       9

Or duplicated with boolean indexing:

df = df[df['id'].duplicated()]
print (df)
   id  values
1   1       6
2   1       3
4   2       6
5   2       3
6   2       9

Detail:

print (df['id'].duplicated())
0    False
1     True
2     True
3    False
4     True
5     True
6     True
Name: id, dtype: bool
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

Another approach:

df.loc[~df.index.isin(df.drop_duplicates(subset='id').index)]
zipa
  • 27,316
  • 6
  • 40
  • 58