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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With