[英]attributes of GroupBy objects in pandas
I believe this piece of code could access name and group attributes using iterator, however, what other attributes of GroupBy objects could I access, and where could I find attributes of GroupBy objects except these two, as I haven't found them on pandas documents.我相信这段代码可以使用迭代器访问名称和组属性,但是,我可以访问 GroupBy 对象的哪些其他属性,以及除了这两个之外我在哪里可以找到 GroupBy 对象的属性,因为我没有在 pandas 文档中找到它们.
for name, group in GroupBy:
GroupBy
returns an GroupBy object , which not only is an iterable containing key/group
tuples, but being an object, you can access its parameters as attributes. GroupBy
返回一个GroupBy object ,它不仅是一个包含key/group
元组的可迭代对象,而且作为一个 object,您可以将其参数作为属性访问。
Let's take the following dataframe as an example:下面以dataframe为例:
df = pd.DataFrame({'a':[1,2,3,2,2,3], 'b':[1,2,3,3,2,1]})
As mentioned, by iterating over the returned object, we get the key/group
tuples that group the dataframe according to the key
:如前所述,通过迭代返回的 object,我们得到了根据
key
对 dataframe 进行分组的key/group
元组:
g = df.groupby('a')
key, group = next(iter(g))
print(key)
# 1
print(group)
a b
0 1 1
This is the what is returned by its __iter__
dunder, next(g.__iter__())
, which calls get_iterator
:这是它的
__iter__
dunder 返回的内容, next(g.__iter__())
,它调用get_iterator
:
def get_iterator(self, data: FrameOrSeries, axis: int = 0):
"""
Groupby iterator
Returns
-------
Generator yielding sequence of (name, subsetted object)
for each group
"""
splitter = self._get_splitter(data, axis=axis)
keys = self._get_group_keys()
for key, (i, group) in zip(keys, splitter):
yield key, group
Its attributes can be accessed just as you would with any other object:可以像访问任何其他 object 一样访问它的属性:
g.__dict__
{'_selection': None,
'level': None,
'as_index': True,
'keys': 'a',
'sort': True,
'group_keys': True,
'squeeze': False,
'observed': False,
'mutated': False,
'obj': a b
0 1 1
1 2 2
2 3 3
3 2 3
4 2 2
5 3 1,
'axis': 0,
'grouper': <pandas.core.groupby.ops.BaseGrouper at 0x17399312eb0>,
'exclusions': {'a'}}
g.sort
# True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.