Python:在 pandas 数据框中基于两列(变量)获取频率计数 [英] Python: get a frequency count based on two columns (variables) in pandas dataframe

查看:1492
本文介绍了Python:在 pandas 数据框中基于两列(变量)获取频率计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我有以下数据框。

 组大小

短小
短小
中等中$ $ b中等小
高大

我想计算多少时间的频率数据框中出现同一行。

 组大小时间

短小2
中等中等1
中等小1
高大1


解决方案

您可以使用groupby的 大小

 在[11]中:df.groupby([Group ,Size])。size()
Out [11]:
组大小
中等中1
小1
短小2
高大1
dtype:i nt64

在[12]中:df.groupby([Group,Size])。size()。reset_index(name =Time)
输出[12]
组大小时间
0中等中1
1中等小1
2短小2
3高大1


Hello I have the following dataframe.

    Group           Size

    Short          Small
    Short          Small
    Moderate       Medium
    Moderate       Small
    Tall           Large

I want to count the frequency of how many time the same row appears in the dataframe.

    Group           Size      Time

    Short          Small        2
    Moderate       Medium       1 
    Moderate       Small        1
    Tall           Large        1

解决方案

You can use groupby's size:

In [11]: df.groupby(["Group", "Size"]).size()
Out[11]:
Group     Size
Moderate  Medium    1
          Small     1
Short     Small     2
Tall      Large     1
dtype: int64

In [12]: df.groupby(["Group", "Size"]).size().reset_index(name="Time")
Out[12]:
      Group    Size  Time
0  Moderate  Medium     1
1  Moderate   Small     1
2     Short   Small     2
3      Tall   Large     1

这篇关于Python:在 pandas 数据框中基于两列(变量)获取频率计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆