get_dummies 和一起计数

get_dummies and count together(get_dummies 和一起计数)
本文介绍了get_dummies 和一起计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,其中包含不同的案例"作为行,其中有一个 id 和一个类别:

I have a dataframe with different "cases" as rows, in which there's an id and a category:

df = DataFrame({ 'id':[1122,3344,5566,5566,3344,5566,1122,3344], 
            'category':['health','transport','energy','energy','transport','transport','transport','energy']})

    category    id
0   health      1122
1   transport   3344
2   energy      5566
3   energy      5566
4   transport   3344
5   transport   5566
6   transport   1122
7   energy      3344

我正在尝试找到一种既可以获取类别的虚拟变量并对其进行计数的好方法,所以通过上面的示例我会得到:

I'm trying to find a good way to both get dummies of the categories and also count them, so with the above example I would get this:

     health  transport  energy
1122    1        1          0
3344    0        2          1
5566    0        1          2

有什么想法吗?

推荐答案

你可以使用pivot_table() 方法:

In [71]: df.pivot_table(index='id', columns='category', aggfunc='size', fill_value=0)
Out[71]:
category  energy  health  transport
id
1122           0       1          1
3344           1       0          2
5566           2       0          1

或:

In [76]: df.pivot_table(index='id', columns='category', aggfunc='size', fill_value=0).rename_axis(None, 1)
Out[76]:
      energy  health  transport
id
1122       0       1          1
3344       1       0          2
5566       2       0          1

这篇关于get_dummies 和一起计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)
cannot use geometry manager pack inside . which already has slaves managed by grid(无法在内部使用几何管理器包。它已经拥有由网格管理的从属对象)
Is there any way to quot;extractquot; the dtype conversion functionality from pandas read_csv?(有没有办法从 pandas Read_CSV中提取数据类型转换功能?)
Highlighting rows based on a condition(根据条件突出显示行)