python - Pandas 中多列的逻辑与

标签 python python-3.x pandas numpy dataframe

我有一个如下所示的数据框(edata)

Domestic   Catsize    Type   Count
   1          0         1      1
   1          1         1      8
   1          0         2      11
   0          1         3      14
   1          1         4      21
   0          1         4      31

从这个数据框中,我想计算所有计数的总和,其中两个变量(Domestic 和 Catsize)的逻辑与结果为零 (0),使得

1   0    0
0   1    0
0   0    0

我用来执行该过程的代码是

g=edata.groupby('Type')
q3=g.apply(lambda x:x[((x['Domestic']==0) & (x['Catsize']==0) |
                       (x['Domestic']==0) & (x['Catsize']==1) |
                       (x['Domestic']==1) & (x['Catsize']==0)
                       )]
            ['Count'].sum()
           )

q3

Type
1     1
2    11
3    14
4    31

此代码工作正常,但是,如果数据框中的变量数量增加,则条件数量会迅速增加。那么,有没有一种聪明的方法来编写一个条件,如果两个(或更多)变量的 AND 运算结果为零,则执行 sum() 函数

最佳答案

您可以先使用 pd.DataFrame.all 进行过滤否定:

cols = ['Domestic', 'Catsize']
res = df[~df[cols].all(1)].groupby('Type')['Count'].sum()

print(res)
# Type
# 1     1
# 2    11
# 3    14
# 4    31
# Name: Count, dtype: int64

关于python - Pandas 中多列的逻辑与,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54156949/

相关文章:

python - 如何在 sqlalchemy execute 中传递原始 sql 中的当前日期

python - Tensorflow 2 中 tf.variable 的条件赋值

Python 退出代码未在 control-m 中捕获

python - 编译 gVim 支持 Python 3

python - Pandas 在 groupby 之后获取所有行的最小值和最大值

python - 如何使用 boto3 检查 S3 key 是否为空?

python - 链接到对象

python - 子类别 Django 商店

python - 从 pandas 数据帧生成边缘列表

Pandas:如何计算周转率?