当 GroupBy 对象可能不包含某个键时,如何避免 Pandas Groupby 键错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35466979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to avoid Pandas Groupby key error when a GroupBy object might not contain a certain key
提问by E. T.
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:
我正在对数据框进行一些分析,其中一列是一个整数,值为 0 或 1(布尔值,但为整数形式)。它看起来像这样:
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"
我根据国籍列对数据进行了分组,其中一个值(上例中的 GB)偶然产生了一个所有成员都只有 1 的组。这造成了一个问题,因为我有一个函数,我称之为包含 group_obj.get_group(0) 的批次,这会导致运行时错误“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.
我正在使用 Python2、Pandas 和 iPython Notebook。
回答by E. T.
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.
因此, groupby 对象中的 keys() 方法已经存储了可用的键,并且可以直接访问它。
回答by Alexander
Use value_counts
, unstack the result to get the results in columns and then use fillna(0)
to replace all NaNs.
使用value_counts
, unstack 结果得到列中的结果,然后使用fillna(0)
来替换所有的 NaN。
>>> df.groupby('Nationality').Result.value_counts().unstack().fillna(0)
Result 0 1
Nationality
CA 1 1
GB 0 2
USA 1 1