gpt4 book ai didi

python - Pandas Groupby : 'observed' parameter with multiple categoricals

转载 作者:行者123 更新时间:2023-12-01 23:01:46 28 4
gpt4 key购买 nike

考虑以下具有两个分类列的 DataFrame:

df = pd.DataFrame({
"state": pd.Categorical(["AK", "AL", "AK", "AL"]),
"gender": pd.Categorical(["M", "M", "M", "F"]),
"name": list("abcd"),
})

df.groupby()中,默认值为observed=Falsedescription对于观察(Pandas 0.25.0)是:

When using a Categorical grouper (as a single grouper, or as part of multiple groupers), the observed keyword controls whether to return a cartesian product of all possible groupers values (observed=False) or only those that are observed groupers (observed=True).

因此,这是我期望的结果:

>>> # Expected result
>>> df.groupby(["state", "gender"])["name"].count()
state gender
AK M 2
F 0
AL F 1
M 1
Name: name, dtype: int64

这是实际结果:

>>> df.groupby(["state", "gender"])["name"].count()
state gender
AK M 2
AL F 1
M 1
Name: name, dtype: int64

我是否误解了这里的描述?

这个解决方法似乎是一个巨大的痛苦,并且正是应该observed=False创建的。我是否缺少替代方案?

>>> idx = pd.MultiIndex.from_product(
... (
... df["state"].cat.categories,
... df["gender"].cat.categories,
... ),
... names=["state", "gender"]
... )
>>> df.groupby(["state", "gender"])["name"].count().reindex(idx).fillna(0.).astype(int)
state gender
AK F 0
M 2
AL F 1
M 1
Name: name, dtype: int64

最佳答案

看起来你放置["name"]的地方把它扔掉了。我认为这有效:

df.groupby(["state", "gender"]).count().fillna(0)["name"]
state gender
AK F 0.0
M 2.0
AL F 1.0
M 1.0
Name: name, dtype: float64

以下是一些有用的变体:

In [16]: df.groupby(["state", "gender"], observed=False).count().fillna(0)["name"].astype(int)
Out[16]:
state gender
AK F 0
M 2
AL F 1
M 1
Name: name, dtype: int64

In [17]: df.groupby(["state", "gender"], observed=True).count()["name"]
Out[17]:
state gender
AK M 2
AL M 1
F 1
Name: name, dtype: int64

关于python - Pandas Groupby : 'observed' parameter with multiple categoricals,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57385009/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com