以orderId分组,计算列表中某一列值为charge和diagnosis的个数
时间: 2024-02-01 17:15:40 浏览: 23
假设数据列表为data,orderId所在列为0,charge所在列为1,diagnosis所在列为2,则可以使用以下代码实现:
```
from collections import defaultdict
# 定义一个字典,key为orderId,value为一个列表,存储该orderId下的charge和diagnosis数量
result = defaultdict(list)
for row in data:
orderId = row[0]
charge = row[1]
diagnosis = row[2]
if charge == 'charge':
result[orderId].append('charge')
if diagnosis == 'diagnosis':
result[orderId].append('diagnosis')
# 打印结果
for orderId, values in result.items():
charge_count = values.count('charge')
diagnosis_count = values.count('diagnosis')
print('orderId:', orderId, 'charge count:', charge_count, 'diagnosis count:', diagnosis_count)
```
运行结果类似如下:
```
orderId: 1 charge count: 2 diagnosis count: 1
orderId: 2 charge count: 1 diagnosis count: 1
orderId: 3 charge count: 2 diagnosis count: 0
```