在Pandas中计算Column为True的地方,由另一个分组

时间:2016-11-08 01:51:34

标签: pandas

给出像这样的DataFrame:

    <html>
    <head>
    <!-- jquery -->
     <script src="//code.jquery.com/jquery-latest.js"></script>

    <!-- featherlight -->
        <script src="//cdnjs.cloudflare.com/ajax/libs/detect_swipe/2.1.3/jquery.detect_swipe.min.js"></script>
        <link href="//cdn.rawgit.com/noelboss/featherlight/1.5.1/release/featherlight.min.css" type="text/css" rel="stylesheet" />
        <script src="//cdn.rawgit.com/noelboss/featherlight/1.5.1/release/featherlight.min.js" type="text/javascript" charset="utf-8"></script>

<!-- featherlight gallery -->

<link href="//cdn.rawgit.com/noelboss/featherlight/1.5.1/release/featherlight.gallery.min.css" type="text/css" rel="stylesheet" />
<script src="//cdn.rawgit.com/noelboss/featherlight/1.5.1/release/featherlight.gallery.min.js" type="text/javascript"></script>
</head>

    <body>  
<a class="thumbnail gallery" href="a.jpg" > <img src="a.jpg" /> </a>
<a class="thumbnail gallery" href="b.jpg" > <img src="b.jpg" /> </a>
<a class="thumbnail gallery" href="c.jpg" > <img src="c.jpg" /> </a>

    <script>
    $(document).ready(function(){
        $('.gallery').featherlightGallery();
    });
    </script>
    </body>
    </html>

看起来像这样:

dft = pd.DataFrame([[12, 'Bob', True], 
                   [123, 'Henry', False], 
                   [768, 'Bob', False]], 
                   columns=['TID', 'UID', 'TRUTH'])

我希望聚合 TID UID TRUTH 0 12 Bob True 1 123 Henry False 3 768 Bob False 并计算UID为真的位置。所以输出应该如下:

TRUTH

我已经尝试过了:

     UID  TRUTH
0    Bob      1
1  Henry      0

我在概念上很难看到如何将条件与聚合放在一起。

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

这是你正在寻找的吗?

print(dft.groupby('UID')['TRUTH'].sum().astype(int))

输出:

UID
Bob      1
Henry    0
Name: TRUTH, dtype: int32