Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid Pandas Groupby key error when a GroupBy object might not contain a certain key

Tags:

python

pandas

I am doing some analysis over a dataframe, with one of the columns being an integer with values either 0 or 1 (Sort of boolean, but in integer form). It looks something like this:

  Nat. | Result
-------|-------
  CA   |  1
  USA  |  0
  GB   |  1
  USA  |  1
  CA   |  0
  GB   |  1

I grouped the data according to the nationality column, and one of the values (GB in the example above) produced -by chance- a group whose all members were only 1. This have created a problem because I have a function that I call a lot that contains group_obj.get_group(0) and this causes a runtime error "KeyError: 0"

My question: I want to create the logic that follows:

if (group_obj contains key 0):
   return group_obj.get_group(0)
else:
   print "Group Object contains no 0s"
   return null

Thanks

I am using Python2, Pandas and iPython Notebook.

like image 310
E. T. Avatar asked Feb 17 '16 20:02

E. T.


2 Answers

OK, so here is how I was able to do it:

if key1 in group_obj.groups.keys():
    #Do processing

so, the keys() method in a groupby object stores already the available keys and it can be accessed directly.

like image 111
E. T. Avatar answered Sep 27 '22 18:09

E. T.


Use value_counts, unstack the result to get the results in columns and then use fillna(0) to replace all NaNs.

>>> df.groupby('Nationality').Result.value_counts().unstack().fillna(0)
Result       0  1
Nationality      
CA           1  1
GB           0  2
USA          1  1
like image 39
Alexander Avatar answered Sep 27 '22 19:09

Alexander