dimanche 10 juillet 2016

Pandas: Can pandas groupby filter work on original object?


Starting with this question as base.

Python Pandas: remove entries based on the number of occurrences

data = pandas.DataFrame(
    {'pid' : [1,1,1,2,2,3,3,3],
     'tag' : [23,45,62,24,45,34,25,62],
     })

   #    pid  tag
   # 0    1   23
   # 1    1   45
   # 2    1   62 
   # 3    2   24
   # 4    2   45
   # 5    3   34
   # 6    3   25
   # 7    3   62

   g = data.groupby('tag')
   g.filter(lambda x: len(x) > 1) # filters out lengths > 1. 

  #     pid  tag
  #  1    1   45
  #  2    1   62
  #  4    2   45
  #  7    3   62


 #This would create a new object g: 
 g  =   g.filter(lambda x: len(x) > 1) #where g is now a dataframe. 

I was wondering is there a way to filter out 'groups' by deleting them from original object g. And, would it be faster than creating a new groupby object from filtered groupby.


Aucun commentaire:

Enregistrer un commentaire