python - 汇总分类变量时超出最大递归深度

标签 python pandas recursion categorical-data depth

当尝试获取 pandas 系列类别的每个级别的计数时,我收到“超出最大递归深度”错误

产生错误的代码示例如下:

vs = pd.Series([0,0,0,1,1,1,1,1]).astype('category')
tn = pd.Series.value_counts(vs)
print(tn[0])
print(tn[1])

产生错误的部分是对 tn[0] 的调用。调用 tn[1] 不会给出此类错误。 如果我添加更多级别,我可以调用下一个级别,但 tn[0] 总是返回错误。 这段代码有什么问题? 我使用的是 Python 3.7.4 和 Pandas 0.25.1。

最佳答案

用途:

vs = pd.Series([0,0,0,1,1,1,1,1]).astype('category')
tn = vs.value_counts()
print(tn)
1    5
0    3
dtype: int64

print(tn[pd.Categorical(0)])
1    5
dtype: int64

print(tn[pd.Categorical(1)])
0    3
dtype: int64

要按位置选择,请使用 Series.ilocSeries.iat :

print(tn.iloc[0])
5
print(tn.iloc[1])
3

关于python - 汇总分类变量时超出最大递归深度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60428707/

相关文章:

python - 如何在 Pandas 的预算表中添加一列增长率?

python-3.x - 使用 pyinstaller 将 python 脚本构建为单个 exe

regex - 在 Scala 中将文件中的字符串与案例类匹配的最佳方法是什么?

python - 返回语句中的时间复杂度为 "or"

python - Python BeautifulSoup 4 文档中给出的示例

python - 在Python中的数据类上使用prefect 2.0流程

python - 防止 Imputer 丢失值

python - lambda 函数可以在 Python 中递归调用自身吗?

python - 获得滚动百分位数排名的快速方法

python - 使用 python 从 .txt 文件中的字符串中提取数字时出现 "Value error: Mixing iteration and read methods would lose data "消息