You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python pandas实现R代码table(iris$Species)的同等效果

对应Python实现代码

你要实现R中统计Species列各分类出现频次的需求,直接对pandas DataFrame的对应列调用value_counts()方法即可:

# 已提前完成iris数据集加载的前提下,直接执行:
iris['Species'].value_counts()

如果需要和R代码的输出顺序(setosa → versicolor → virginica)完全对齐,可以额外加索引重排:

iris['Species'].value_counts().reindex(['setosa', 'versicolor', 'virginica'])

输出效果

运行后得到的结果和R代码完全一致:

setosa        50
versicolor    50
virginica     50
Name: Species, dtype: int64

内容的提问来源于stack exchange,提问作者Hyeyeon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 11:30:04