如何用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
相关产品推荐
相关产品推荐

