<small id='ujBv8'></small><noframes id='ujBv8'>

    <i id='ujBv8'><tr id='ujBv8'><dt id='ujBv8'><q id='ujBv8'><span id='ujBv8'><b id='ujBv8'><form id='ujBv8'><ins id='ujBv8'></ins><ul id='ujBv8'></ul><sub id='ujBv8'></sub></form><legend id='ujBv8'></legend><bdo id='ujBv8'><pre id='ujBv8'><center id='ujBv8'></center></pre></bdo></b><th id='ujBv8'></th></span></q></dt></tr></i><div id='ujBv8'><tfoot id='ujBv8'></tfoot><dl id='ujBv8'><fieldset id='ujBv8'></fieldset></dl></div>
      <bdo id='ujBv8'></bdo><ul id='ujBv8'></ul>
    <legend id='ujBv8'><style id='ujBv8'><dir id='ujBv8'><q id='ujBv8'></q></dir></style></legend>
      <tfoot id='ujBv8'></tfoot>

      pandas 聚合的条件总和

      时间:2023-09-13
    1. <legend id='tZ3dH'><style id='tZ3dH'><dir id='tZ3dH'><q id='tZ3dH'></q></dir></style></legend>

            <tbody id='tZ3dH'></tbody>

                <bdo id='tZ3dH'></bdo><ul id='tZ3dH'></ul>

                <small id='tZ3dH'></small><noframes id='tZ3dH'>

                <i id='tZ3dH'><tr id='tZ3dH'><dt id='tZ3dH'><q id='tZ3dH'><span id='tZ3dH'><b id='tZ3dH'><form id='tZ3dH'><ins id='tZ3dH'></ins><ul id='tZ3dH'></ul><sub id='tZ3dH'></sub></form><legend id='tZ3dH'></legend><bdo id='tZ3dH'><pre id='tZ3dH'><center id='tZ3dH'></center></pre></bdo></b><th id='tZ3dH'></th></span></q></dt></tr></i><div id='tZ3dH'><tfoot id='tZ3dH'></tfoot><dl id='tZ3dH'><fieldset id='tZ3dH'></fieldset></dl></div>
              • <tfoot id='tZ3dH'></tfoot>
                本文介绍了pandas 聚合的条件总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我最近刚刚从 R 切换到 python,并且在再次习惯数据帧而不是使用 R 的 data.table 时遇到了一些麻烦.我遇到的问题是我想获取一个字符串列表,检查一个值,然后将该字符串的计数相加 - 由用户分解.所以我想把这些数据:

                I just recently made the switch from R to python and have been having some trouble getting used to data frames again as opposed to using R's data.table. The problem I've been having is that I'd like to take a list of strings, check for a value, then sum the count of that string- broken down by user. So I would like to take this data:

                   A_id       B    C
                1:   a1    "up"  100
                2:   a2  "down"  102
                3:   a3    "up"  100
                3:   a3    "up"  250
                4:   a4  "left"  100
                5:   a5 "right"  102
                

                然后返回:

                   A_id_grouped   sum_up   sum_down  ...  over_200_up
                1:           a1        1          0  ...            0
                2:           a2        0          1                 0
                3:           a3        2          0  ...            1
                4:           a4        0          0                 0
                5:           a5        0          0  ...            0
                

                在我用 R 代码做之前(使用 data.table)

                Before I did it with the R code (using data.table)

                >DT[ ,list(A_id_grouped, sum_up = sum(B == "up"),
                +  sum_down = sum(B == "down"), 
                +  ...,
                +  over_200_up = sum(up == "up" & < 200), by=list(A)];
                

                但是,我最近使用 Python 的所有尝试都失败了:

                However all of my recent attempts with Python have failed me:

                DT.agg({"D": [np.sum(DT[DT["B"]=="up"]),np.sum(DT[DT["B"]=="up"])], ...
                    "C": np.sum(DT[(DT["B"]=="up") & (DT["C"]>200)])
                    })
                

                提前感谢您!这似乎是一个简单的问题,但我在任何地方都找不到.

                Thank you in advance! it seems like a simple question however I couldn't find it anywhere.

                推荐答案

                为了补充 unutbu 的答案,这里有一个在 groupby 对象上使用 apply 的方法.

                To complement unutbu's answer, here's an approach using apply on the groupby object.

                >>> df.groupby('A_id').apply(lambda x: pd.Series(dict(
                    sum_up=(x.B == 'up').sum(),
                    sum_down=(x.B == 'down').sum(),
                    over_200_up=((x.B == 'up') & (x.C > 200)).sum()
                )))
                      over_200_up  sum_down  sum_up
                A_id                               
                a1              0         0       1
                a2              0         1       0
                a3              1         0       2
                a4              0         0       0
                a5              0         0       0
                

                这篇关于pandas 聚合的条件总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:为什么在 2012 年 python 中的 pandas 合并比 R 中的 下一篇:如何在 CentOS 上将 Python3.5.2 设置为默认 Python 版本

                相关文章

                最新文章

                1. <tfoot id='DgopI'></tfoot><legend id='DgopI'><style id='DgopI'><dir id='DgopI'><q id='DgopI'></q></dir></style></legend>

                    <bdo id='DgopI'></bdo><ul id='DgopI'></ul>

                  <i id='DgopI'><tr id='DgopI'><dt id='DgopI'><q id='DgopI'><span id='DgopI'><b id='DgopI'><form id='DgopI'><ins id='DgopI'></ins><ul id='DgopI'></ul><sub id='DgopI'></sub></form><legend id='DgopI'></legend><bdo id='DgopI'><pre id='DgopI'><center id='DgopI'></center></pre></bdo></b><th id='DgopI'></th></span></q></dt></tr></i><div id='DgopI'><tfoot id='DgopI'></tfoot><dl id='DgopI'><fieldset id='DgopI'></fieldset></dl></div>

                  <small id='DgopI'></small><noframes id='DgopI'>