基于从其他数据创建的条件从数据集获取计数

2024-06-24 13:05:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样一个数据集:

Primary   col1   col2   
nex1    nex2    nex3    
nex2    nex1    nex3    
nex3    nex1    nex2    

我想为每个rowxcol组合循环这个数据集,并计算所有情况,例如where(nex1>=1和nex2>=1) 在另一个包含这些nex1-nex3000变量的数据集(data_y)中。然后移到第二种情况(数据x),其中(nex1>=1和nex3>=1) 直到排的最后。 现在转到第二行,重复,直到最后一行。所以数据就是为了得到所有可能的病例。我想从另一个数据集获取这些案例的计数

数据类型如下:

nex1 nex2 nex3.....nex3000
1     0    1     3
0     0    0     0
3     1    0     1
1     2    1     0
0     0    1     0

所以最终的数据集将有这些案例的计数(每个案例来自rowxcol组合的数据x)

 col1   col2...
  2      2
  2      1
  2      1...        

所以伪代码是这样的:

for each primary in data_x:
     for each col in data_x:
        select count(*) from data_y where &primary_val>=1 and &col_val>=1

第一种情况示例:

 first primary in data_x(is nex1):
         first col in data_x(nex2):
               select count(*) from data_y where nex1>=1 and nex2>=1

这将为输出数据集col1:2中的第一个case生成(数据集中有两个case满足第一个主x col条件)


Tags: 数据ingtdata情况colwhere案例