python - 如何检查pandas组中n个正值的数量

标签 python pandas pandas-groupby

我有一个看起来像这样的数据框

pd.DataFrame({'a': ['cust1', 'cust1', 'cust1', 'cust1', 'cust2', 'cust2', 'cust2', 'cust2', 'cust3', 'cust3', 'cust3', 'cust3'],
                   'year': [2017, 2018, 2019, 2020, 2017, 2018, 2019, 2020, 2017, 2018, 2019, 2020],
                   'amt': [2, 0, 4, 'NaN', 2, 2, 3, 3, 3, 2, 'NaN', 5]})

        a  year  amt
0   cust1  2017    2
1   cust1  2018    0
2   cust1  2019    4
3   cust1  2020  NaN
4   cust2  2017    2
5   cust2  2018    2
6   cust2  2019    3
7   cust2  2020    3
8   cust3  2017    3
9   cust3  2018    2
10  cust3  2019  NaN
11  cust3  2020    5

我需要检查“a”列中每组的“amt”列中是否至少有 3 个正值。生成的数据框应如下所示

        a  year  amt   cond
0   cust1  2017    2  False
1   cust1  2018    0  False
2   cust1  2019    4  False
3   cust1  2020  NaN  False
4   cust2  2017    2   True
5   cust2  2018    2   True
6   cust2  2019    3   True
7   cust2  2020    3   True
8   cust3  2017    3   True
9   cust3  2018    2   True
10  cust3  2019  NaN   True
11  cust3  2020    5   True

以下逻辑适用:

cust1 = False,因为只有 2 个正值(2017、2019)

cust2 = True 为 4 个正值

cust3 = True 为 3 个正值

最佳答案

让我们尝试使用 sum 进行transform

df = df.replace('NaN',np.nan)
df['cond'] = df.amt.gt(0).groupby(df['a']).transform('sum')>2
df
Out[62]: 
        a  year  amt   cond
0   cust1  2017  2.0  False
1   cust1  2018  0.0  False
2   cust1  2019  4.0  False
3   cust1  2020  NaN  False
4   cust2  2017  2.0   True
5   cust2  2018  2.0   True
6   cust2  2019  3.0   True
7   cust2  2020  3.0   True
8   cust3  2017  3.0   True
9   cust3  2018  2.0   True
10  cust3  2019  NaN   True
11  cust3  2020  5.0   True

关于python - 如何检查pandas组中n个正值的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63344382/

相关文章:

python - 在 Debug模式下切换断点时 Pycharm 运行缓慢

python - 在 neo4j 中存储(和查询)python ipaddress 数据类型

python - Pandas 误解 CSV 文件中的日期列

python - 在 Python 中查询数据库并存储到数据结构中

python-3.x - 有没有办法计算 Pandas 数据框中不同行数的前瞻性滚动值?

python - 基于月份的累计列数

python - 为什么我在两种方式之间得到长度差异 : dict and array

python - 在 Python/matplotlib 中使用 xaxis_date() 手动设置 xticks

python - 当某些列值为空时,如何合并 Dataframe 中的多行?

python - 使用 pandas 根据键变量将多行转换为单行