小编Dom*_*ikS的帖子

熊猫:选择groupby.sum()满足条件的行

在熊猫中,我有以下形式的数据框:

>>> import pandas as pd  
>>> df = pd.DataFrame({'ID':[51,51,51,24,24,24,31], 'x':[0,1,0,0,1,1,0]})
>>> df

ID   x
51   0
51   1
51   0
24   0
24   1
24   1
31   0
Run Code Online (Sandbox Code Playgroud)

对于每个“ ID”,多次记录“ x”的值,它的值是0或1。我想从中选择那些df包含“ x”至少为1的“ ID”的行。

对于每个“ ID”,我设法计算“ x”为1的次数,

>>> df.groupby('ID')['x'].sum()

ID
51    1
24    2
31    0
Run Code Online (Sandbox Code Playgroud)

但是我不知道如何从这里开始。我想要以下输出:

ID   x
24   0
24   1
24   1
Run Code Online (Sandbox Code Playgroud)

python pandas pandas-groupby

5
推荐指数
2
解决办法
2755
查看次数

在类中初始化类

我是C++的新手,我的问题可能很简单,但我无法找到解决方案.

我有两节课,SL.S看起来像这样:

class S
{
private:
    int m_x;

public:
    S(int x) : m_x(x)
    {
    }

    int m_y = 2*m_x;   // some random action in S
};
Run Code Online (Sandbox Code Playgroud)

现在我有了第二个类L,我想初始化一个S-object:

class L
{
private:
    S s(10);   // 10 is just some random value
    int m_y;
public:
    L(int y): m_y(y)
    {
    }

// ignore the rest for now.
};
Run Code Online (Sandbox Code Playgroud)

这会error: expected identifier before numeric constant在初始化行产生错误s(10).

我不明白为什么我不能这样做.我怎么能解决这个问题?如果我想初始化对象S s(m_y)怎么办?

c++ initialization class

0
推荐指数
1
解决办法
491
查看次数

标签 统计

c++ ×1

class ×1

initialization ×1

pandas ×1

pandas-groupby ×1

python ×1