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

如何用Pandas Python统计Category列的总单元格数量

问题:用Pandas统计Category列的总单元格数量

我想用Pandas统计DataFrame中Category列的所有单元格总数,尝试用df['Category'].value_counts()得到了各分类的出现次数(如下),但我需要的是把这些次数求和的总数量,该怎么操作?

已尝试的输出:

Engineering & Information Technology        1159
Manufacturing                               1044
Vehicle Service                              915
Supply Chain                                 378
Energy - Solar & Storage                     374
Construction & Facilities                    296
Sales & Customer Support                     269
Finance                                      119
Charging                                     115
Environmental, Health & Safety                93
Autopilot & Robotics                          78
Operations & Business Support                 75
HR                                            64
Design                                        59
Vehicle Software                              40
Legal & Government Affairs                    18
External Relations & Employee Experience       2
Name: Category, dtype: int64

原始数据示例:

Title   Category    Location
0   Technical Product Analyst   Engineering & Information Technology    Draper, Utah
1   Software Engineer   Engineering & Information Technology    Austin, Texas
2   Software Development Engineer   Engineering & Information Technology    Fremont, California
3   Global Supply Analyst   Supply Chain    Palo Alto, California
4   Software Support Engineer, Battery Automation ...   Engineering & Information Technology    Austin, Texas

解决方法

  • 直接统计列的非空行数
    无需先执行value_counts,直接调用count()方法就能得到Category列的非空单元格总数,这是最直接高效的方式:

    df['Category'].count()
    
  • 对value_counts结果求和
    如果你已经生成了value_counts的结果,可以直接对其求和得到总数:

    df['Category'].value_counts().sum()
    
  • 通过DataFrame总行数获取
    若确认Category列没有缺失值,也可以直接取DataFrame的总行数:

    df.shape[0]
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 08:31:18