JPV
JPV

Reputation: 1079

Dataframe grouping and deleting rows

I have grouped a dataframe with two criteria: years and group(1,2,3,4) as follows

df.groupby([df.index.year,'Group']).count()['Column1']
              Group
2009  1            1
      2            2
      3            8
      4            0
2010  1            3
      2            1
      3            9
      4            2

I would like to get rid of group 4 from year 2009 directly from the grouped data. thanks

Upvotes: 1

Views: 100

Answers (2)

Scott Boston
Scott Boston

Reputation: 153500

You can use drop with tuple:

df1 = df.groupby([df.index.year,'Group']).count()['Column1']

df1 = df1.drop((2009,4))

OR

df1.drop((2009,4), inplace=True)

Upvotes: 1

Allen Qin
Allen Qin

Reputation: 19957

Will this work?

df.groupby([df.index.year,'Group']).count()['Column1'].filter(lambda x: x.index[0]!=2009 or max(x.Group)!=4)

Upvotes: 0

Related Questions