You need to enable JavaScript to run this app. 为什么object类型的pandas Series调用value_counts返回dtype为int64
优惠活动
大模型
产品
解决方案
定价
更多

为什么object类型的pandas Series调用value_counts返回dtype为int64

原因说明

Series.value_counts()返回的是一个全新的Series对象,你看到的dtype: int64是这个新Series的值类型,不是原Sales列值的类型:

  • 新返回Series的索引存储的是原Sales列的所有唯一值,这些值的类型和原列一致为object,你可以执行res = df['Sales'].value_counts(dropna=False)后,通过res.index.dtype验证索引的类型确实是object。
  • 新返回Series的值存储的是每个唯一值出现的计数频次,频次本身是正整数,所以值的类型默认为int64,这就是你在结果末尾看到的dtype标注的来源。

举个直观的对应示例,你给出的返回结果:

df['Sales'].value_counts(dropna=False)
...
12400 3
12000 1
Name: Sales, dtype: int64

其中12400、12000是索引(对应原Sales列的唯一值),3、1是值(对应出现频次),dtype: int64描述的就是3、1这部分值的类型。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.08 14:45:01