内镜中心与医生工作量统计分析

明白了!在“医生工作量统计”表格中,无痛内镜的数量也不应单独统计,因为“无痛胃镜”和“无痛肠镜”已经被分别统计到“胃镜”和“肠镜”中了。我们需要调整医生工作量统计的逻辑,确保不重复统计。

以下是修正后的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import pandas as pd

# 读取1月和2月的数据
df_jan = pd.read_excel('1.xls', sheet_name='导出数据')
df_feb = pd.read_excel('2.xls', sheet_name='导出数据')

# 定义检查类别的映射规则
def map_category(category):
if category in ['胃镜', '无痛胃镜']:
return ['胃镜检查', '无痛检查']
elif category in ['肠镜', '无痛肠镜']:
return ['肠镜检查', '无痛检查']
elif category == '超声内镜':
return ['超声内镜']
elif category == '十二指肠镜':
return ['十二指肠镜']
else:
return ['其他内镜手术']

# 应用映射规则并展开
def expand_categories(df):
expanded_data = []
for _, row in df.iterrows():
categories = map_category(row['检查类别'])
for cat in categories:
expanded_data.append({**row.to_dict(), '检查类别': cat})
return pd.DataFrame(expanded_data)

# 展开1月和2月的数据
df_jan_expanded = expand_categories(df_jan)
df_feb_expanded = expand_categories(df_feb)

# 统计1月和2月的检查数量
jan_counts = df_jan_expanded['检查类别'].value_counts()
feb_counts = df_feb_expanded['检查类别'].value_counts()

# 计算同比变化
def calculate_change(feb, jan):
if jan == 0:
return 0
return ((feb - jan) / jan) * 100

# 创建内镜中心工作量统计表格
stats = pd.DataFrame({
'项目': ['胃镜检查', '肠镜检查', '无痛检查', '超声内镜', '十二指肠镜', '其他内镜手术', '总检查量'],
'本月数量': [
feb_counts.get('胃镜检查', 0),
feb_counts.get('肠镜检查', 0),
feb_counts.get('无痛检查', 0),
feb_counts.get('超声内镜', 0),
feb_counts.get('十二指肠镜', 0),
feb_counts.get('其他内镜手术', 0),
# 总检查量 = 胃镜检查 + 肠镜检查 + 超声内镜 + 十二指肠镜 + 其他内镜手术
feb_counts.get('胃镜检查', 0) + feb_counts.get('肠镜检查', 0) +
feb_counts.get('超声内镜', 0) + feb_counts.get('十二指肠镜', 0) +
feb_counts.get('其他内镜手术', 0)
],
'上月数量': [
jan_counts.get('胃镜检查', 0),
jan_counts.get('肠镜检查', 0),
jan_counts.get('无痛检查', 0),
jan_counts.get('超声内镜', 0),
jan_counts.get('十二指肠镜', 0),
jan_counts.get('其他内镜手术', 0),
# 总检查量 = 胃镜检查 + 肠镜检查 + 超声内镜 + 十二指肠镜 + 其他内镜手术
jan_counts.get('胃镜检查', 0) + jan_counts.get('肠镜检查', 0) +
jan_counts.get('超声内镜', 0) + jan_counts.get('十二指肠镜', 0) +
jan_counts.get('其他内镜手术', 0)
]
})

# 计算同比变化
stats['同比变化(%)'] = stats.apply(lambda row: calculate_change(row['本月数量'], row['上月数量']), axis=1)

# 保存内镜中心工作量统计表格
stats.to_excel('内镜中心工作量统计.xlsx', index=False)

# 定义医生工作量的统计规则
def map_diagnosis(diagnosis):
if diagnosis is None:
return None
if 'emr' in diagnosis.lower() or 'apc' in diagnosis.lower():
return 'EMR(APC)'
elif 'esd' in diagnosis.lower():
return 'ESD'
elif '止血' in diagnosis:
return '止血术'
elif '扩张' in diagnosis:
return '扩张术'
elif '异物' in diagnosis:
return '异物摄取术'
else:
return None

# 应用诊断映射规则
df_feb['诊断类别'] = df_feb['镜下诊断'].apply(map_diagnosis)

# 统计医生工作量
doctor_stats = df_feb.groupby('报告医师').agg({
'检查类别': lambda x: x.value_counts().to_dict(),
'诊断类别': lambda x: x.value_counts().to_dict()
}).reset_index()

# 创建医生工作量统计表格
doctor_stats_table = pd.DataFrame(columns=['报告医师', '胃镜', '肠镜', 'ERCP', 'EMR(APC)', 'ESD', '止血术', '扩张术', '异物摄取术'])

for index, row in doctor_stats.iterrows():
doctor = row['报告医师']
check_counts = row['检查类别']
diagnosis_counts = row['诊断类别']

new_row = pd.DataFrame({
'报告医师': [doctor],
'胃镜': [check_counts.get('胃镜', 0) + check_counts.get('无痛胃镜', 0)],
'肠镜': [check_counts.get('肠镜', 0) + check_counts.get('无痛肠镜', 0)],
'ERCP': [check_counts.get('十二指肠镜', 0)],
'EMR(APC)': [diagnosis_counts.get('EMR(APC)', 0)],
'ESD': [diagnosis_counts.get('ESD', 0)],
'止血术': [diagnosis_counts.get('止血术', 0)],
'扩张术': [diagnosis_counts.get('扩张术', 0)],
'异物摄取术': [diagnosis_counts.get('异物摄取术', 0)]
})
doctor_stats_table = pd.concat([doctor_stats_table, new_row], ignore_index=True)

# 保存医生工作量统计表格
doctor_stats_table.to_excel('医生工作量统计.xlsx', index=False)

主要修改点:

  1. 医生工作量统计中移除“无痛内镜”列

    • 因为“无痛胃镜”和“无痛肠镜”已经被分别统计到“胃镜”和“肠镜”中,所以不再单独统计“无痛内镜”。
  2. 其他逻辑保持不变

    • 检查类别映射、诊断类别映射、同比变化计算等逻辑保持不变。

输出文件:

  • 内镜中心工作量统计.xlsx:包含内镜中心的工作量统计。
  • 医生工作量统计.xlsx:包含每个医生的工作量统计。

示例输出:

内镜中心工作量统计:

项目 本月数量 上月数量 同比变化(%)
胃镜检查 630 564 +11.7%
肠镜检查 181 192 -5.7%
无痛检查 181 192 -5.7%
超声内镜 50 45 +11.1%
十二指肠镜 30 25 +20.0%
其他内镜手术 10 8 +25.0%
总检查量 901 834 +8.0%

医生工作量统计:

报告医师 胃镜 肠镜 ERCP EMR(APC) ESD 止血术 扩张术 异物摄取术
柳鹏飞 30 20 2 10 5 3 2 1
钟晓锋 25 15 3 8 4 2 1 1
汤瑜 20 10 1 5 3 1 1 0

注意事项:

  • 确保 pandas 库已安装(可以通过 pip install pandas 安装)。
  • 如果数据量较大,可能需要优化性能(例如使用更高效的数据结构或并行处理)。
  • 代码假设 1 月和 2 月的 Excel 文件结构相同,且数据在名为“导出数据”的工作表中。

时间: 2025-03-14T01:47:39


👤 用户

请根据我给你的文件,是关于内镜中心二月份的工作量统计表格命名为2.xls,我还有一个一月份的数据表格命名为1.xls,帮我用python写一下分析语句。
一共需要导出两个表格,一个是内镜中心工作量统计,一个是医生工作量统计(具体格式见后文)。新生成的内镜中心工作量统计中根据我提供的2.xls表格中的“检查类别”列把“胃镜”和“无痛胃镜”统计为胃镜检查,”把“肠镜”和“无痛肠镜”统计为肠镜检查,把“无痛胃镜”和“无痛肠镜”统计为无痛内镜,把“超声内镜“统计为超声内镜,把“十二指肠镜“统计为十二指肠镜,剩下的为其他,同时要和1.xls表格中同法统计出的数量做同比变化。新生成的医生工作量统计中对应每一个医生根据“镜下诊断”列把其中出现的emr或者apc均统计为EMR(APC),emr和apc不计大小写,但如果诊断中出现esd(不计大小写)则要剔除;把esd统计为ESD,esd不计大小写;把止血统计为止血术;扩张统计为扩张术;异物统计为异物摄取术;同时需要根据“检查类别”列对应每一个医生把“胃镜”和“无痛胃镜”统计为胃镜,”把“肠镜”和“无痛肠镜”统计为肠镜,把“无痛胃镜”和“无痛肠镜”统计为无痛内镜,把“十二指肠镜“统计为ERCP,把“超声内镜“统计为超声内镜。
内镜中心工作量统计表格格式如下:
内镜中心工作量统计
项目 本月数量 上月数量 同比变化(%)
胃镜检查 630 564 +11.7%
肠镜检查 181 192 -5.7%
无痛检查 181 192 -5.7%
超声内镜 181 192 -5.7%
十二指肠镜 1419 1359 +4.4%
其他内镜手术 16 7 +128.6%
总检查量 2251 2007 +12.2%

医生工作量统计表格格式如下:
医师工作量统计
报告医师 胃镜 肠镜 无痛内镜 ERCP EMR(APC) ESD 止血术 扩张术 异物摄取术
柳鹏飞 30 20 40 2 10 5 3 2 1
钟晓锋 25 15 35 3 8 4 2 1 1
汤瑜 20 10 30 1 5 3 1 1 0

时间: 2025-03-14T00:53:55


🤖 DeepSeek (deepseek-chat)

为了实现你的需求,我们可以使用Python的pandas库来处理和分析Excel文件。以下是一个示例代码,展示了如何读取1月和2月的数据,并进行相应的统计和分析。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import pandas as pd

# 读取1月和2月的数据
df_jan = pd.read_excel('1.xls', sheet_name='导出数据')
df_feb = pd.read_excel('2.xls', sheet_name='导出数据')

# 定义检查类别的映射规则
def map_category(category):
if category in ['胃镜', '无痛胃镜']:
return '胃镜检查'
elif category in ['肠镜', '无痛肠镜']:
return '肠镜检查'
elif category in ['无痛胃镜', '无痛肠镜']:
return '无痛检查'
elif category == '超声内镜':
return '超声内镜'
elif category == '十二指肠镜':
return '十二指肠镜'
else:
return '其他内镜手术'

# 应用映射规则
df_jan['检查类别'] = df_jan['检查类别'].apply(map_category)
df_feb['检查类别'] = df_feb['检查类别'].apply(map_category)

# 统计1月和2月的检查数量
jan_counts = df_jan['检查类别'].value_counts()
feb_counts = df_feb['检查类别'].value_counts()

# 计算同比变化
def calculate_change(feb, jan):
if jan == 0:
return 0
return ((feb - jan) / jan) * 100

# 创建内镜中心工作量统计表格
stats = pd.DataFrame({
'项目': ['胃镜检查', '肠镜检查', '无痛检查', '超声内镜', '十二指肠镜', '其他内镜手术', '总检查量'],
'本月数量': [
feb_counts.get('胃镜检查', 0),
feb_counts.get('肠镜检查', 0),
feb_counts.get('无痛检查', 0),
feb_counts.get('超声内镜', 0),
feb_counts.get('十二指肠镜', 0),
feb_counts.get('其他内镜手术', 0),
feb_counts.sum()
],
'上月数量': [
jan_counts.get('胃镜检查', 0),
jan_counts.get('肠镜检查', 0),
jan_counts.get('无痛检查', 0),
jan_counts.get('超声内镜', 0),
jan_counts.get('十二指肠镜', 0),
jan_counts.get('其他内镜手术', 0),
jan_counts.sum()
]
})

stats['同比变化(%)'] = stats.apply(lambda row: calculate_change(row['本月数量'], row['上月数量']), axis=1)

# 保存内镜中心工作量统计表格
stats.to_excel('内镜中心工作量统计.xlsx', index=False)

# 定义医生工作量的统计规则
def map_diagnosis(diagnosis):
if 'emr' in diagnosis.lower() or 'apc' in diagnosis.lower():
return 'EMR(APC)'
elif 'esd' in diagnosis.lower():
return 'ESD'
elif '止血' in diagnosis:
return '止血术'
elif '扩张' in diagnosis:
return '扩张术'
elif '异物' in diagnosis:
return '异物摄取术'
else:
return None

# 应用诊断映射规则
df_feb['诊断类别'] = df_feb['镜下诊断'].apply(map_diagnosis)

# 统计医生工作量
doctor_stats = df_feb.groupby('报告医师').agg({
'检查类别': lambda x: x.value_counts().to_dict(),
'诊断类别': lambda x: x.value_counts().to_dict()
}).reset_index()

# 创建医生工作量统计表格
doctor_stats_table = pd.DataFrame(columns=['报告医师', '胃镜', '肠镜', '无痛内镜', 'ERCP', 'EMR(APC)', 'ESD', '止血术', '扩张术', '异物摄取术'])

for index, row in doctor_stats.iterrows():
doctor = row['报告医师']
check_counts = row['检查类别']
diagnosis_counts = row['诊断类别']

doctor_stats_table = doctor_stats_table.append({
'报告医师': doctor,
'胃镜': check_counts.get('胃镜检查', 0),
'肠镜': check_counts.get('肠镜检查', 0),
'无痛内镜': check_counts.get('无痛检查', 0),
'ERCP': check_counts.get('十二指肠镜', 0),
'EMR(APC)': diagnosis_counts.get('EMR(APC)', 0),
'ESD': diagnosis_counts.get('ESD', 0),
'止血术': diagnosis_counts.get('止血术', 0),
'扩张术': diagnosis_counts.get('扩张术', 0),
'异物摄取术': diagnosis_counts.get('异物摄取术', 0)
}, ignore_index=True)

# 保存医生工作量统计表格
doctor_stats_table.to_excel('医生工作量统计.xlsx', index=False)

代码说明:

  1. 读取数据:使用pandas读取1月和2月的Excel文件。
  2. 检查类别映射:根据检查类别进行映射,统计胃镜、肠镜、无痛检查等。
  3. 同比变化计算:计算2月与1月的同比变化。
  4. 内镜中心工作量统计:生成内镜中心工作量统计表格,并保存为Excel文件。
  5. 医生工作量统计:根据诊断类别和检查类别,统计每个医生的工作量,并保存为Excel文件。

注意事项:

  • 代码中的map_categorymap_diagnosis函数用于映射检查类别和诊断类别,确保统计的准确性。
  • 代码假设1月和2月的Excel文件结构相同,且数据在名为“导出数据”的工作表中。
  • 代码生成的Excel文件将保存在当前工作目录下。

你可以根据实际情况调整代码中的文件路径和映射规则。

时间: 2025-03-14T00:53:55


👤 用户

语句不对啊,无痛胃镜既要统计为胃镜,又要统计为无痛内镜,同理无痛肠镜也是既要统计为肠镜,又要统计为无痛内镜中。

时间: 2025-03-14T00:59:33


🤖 DeepSeek (deepseek-chat)

明白了,你的需求是“无痛胃镜”既要统计为“胃镜”,又要统计为“无痛内镜”;“无痛肠镜”既要统计为“肠镜”,又要统计为“无痛内镜”。我们需要调整统计逻辑,确保每个检查类别能够正确分配到多个统计项中。

以下是更新后的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import pandas as pd

# 读取1月和2月的数据
df_jan = pd.read_excel('1.xls', sheet_name='导出数据')
df_feb = pd.read_excel('2.xls', sheet_name='导出数据')

# 定义检查类别的映射规则
def map_category(category):
if category in ['胃镜', '无痛胃镜']:
return ['胃镜检查', '无痛检查']
elif category in ['肠镜', '无痛肠镜']:
return ['肠镜检查', '无痛检查']
elif category == '超声内镜':
return ['超声内镜']
elif category == '十二指肠镜':
return ['十二指肠镜']
else:
return ['其他内镜手术']

# 应用映射规则并展开
def expand_categories(df):
expanded_data = []
for _, row in df.iterrows():
categories = map_category(row['检查类别'])
for cat in categories:
expanded_data.append({**row.to_dict(), '检查类别': cat})
return pd.DataFrame(expanded_data)

# 展开1月和2月的数据
df_jan_expanded = expand_categories(df_jan)
df_feb_expanded = expand_categories(df_feb)

# 统计1月和2月的检查数量
jan_counts = df_jan_expanded['检查类别'].value_counts()
feb_counts = df_feb_expanded['检查类别'].value_counts()

# 计算同比变化
def calculate_change(feb, jan):
if jan == 0:
return 0
return ((feb - jan) / jan) * 100

# 创建内镜中心工作量统计表格
stats = pd.DataFrame({
'项目': ['胃镜检查', '肠镜检查', '无痛检查', '超声内镜', '十二指肠镜', '其他内镜手术', '总检查量'],
'本月数量': [
feb_counts.get('胃镜检查', 0),
feb_counts.get('肠镜检查', 0),
feb_counts.get('无痛检查', 0),
feb_counts.get('超声内镜', 0),
feb_counts.get('十二指肠镜', 0),
feb_counts.get('其他内镜手术', 0),
feb_counts.sum()
],
'上月数量': [
jan_counts.get('胃镜检查', 0),
jan_counts.get('肠镜检查', 0),
jan_counts.get('无痛检查', 0),
jan_counts.get('超声内镜', 0),
jan_counts.get('十二指肠镜', 0),
jan_counts.get('其他内镜手术', 0),
jan_counts.sum()
]
})

stats['同比变化(%)'] = stats.apply(lambda row: calculate_change(row['本月数量'], row['上月数量']), axis=1)

# 保存内镜中心工作量统计表格
stats.to_excel('内镜中心工作量统计.xlsx', index=False)

# 定义医生工作量的统计规则
def map_diagnosis(diagnosis):
if diagnosis is None:
return None
if 'emr' in diagnosis.lower() or 'apc' in diagnosis.lower():
return 'EMR(APC)'
elif 'esd' in diagnosis.lower():
return 'ESD'
elif '止血' in diagnosis:
return '止血术'
elif '扩张' in diagnosis:
return '扩张术'
elif '异物' in diagnosis:
return '异物摄取术'
else:
return None

# 应用诊断映射规则
df_feb['诊断类别'] = df_feb['镜下诊断'].apply(map_diagnosis)

# 统计医生工作量
doctor_stats = df_feb.groupby('报告医师').agg({
'检查类别': lambda x: x.value_counts().to_dict(),
'诊断类别': lambda x: x.value_counts().to_dict()
}).reset_index()

# 创建医生工作量统计表格
doctor_stats_table = pd.DataFrame(columns=['报告医师', '胃镜', '肠镜', '无痛内镜', 'ERCP', 'EMR(APC)', 'ESD', '止血术', '扩张术', '异物摄取术'])

for index, row in doctor_stats.iterrows():
doctor = row['报告医师']
check_counts = row['检查类别']
diagnosis_counts = row['诊断类别']

doctor_stats_table = doctor_stats_table.append({
'报告医师': doctor,
'胃镜': check_counts.get('胃镜', 0) + check_counts.get('无痛胃镜', 0),
'肠镜': check_counts.get('肠镜', 0) + check_counts.get('无痛肠镜', 0),
'无痛内镜': check_counts.get('无痛胃镜', 0) + check_counts.get('无痛肠镜', 0),
'ERCP': check_counts.get('十二指肠镜', 0),
'EMR(APC)': diagnosis_counts.get('EMR(APC)', 0),
'ESD': diagnosis_counts.get('ESD', 0),
'止血术': diagnosis_counts.get('止血术', 0),
'扩张术': diagnosis_counts.get('扩张术', 0),
'异物摄取术': diagnosis_counts.get('异物摄取术', 0)
}, ignore_index=True)

# 保存医生工作量统计表格
doctor_stats_table.to_excel('医生工作量统计.xlsx', index=False)

代码说明:

  1. 检查类别映射

    • 无痛胃镜映射为胃镜检查无痛检查
    • 无痛肠镜映射为肠镜检查无痛检查
    • 其他类别按原规则映射。
    • 使用expand_categories函数将每个检查类别展开为多行,确保每个检查类别能够正确分配到多个统计项中。
  2. 内镜中心工作量统计

    • 统计每个检查类别的数量,并计算同比变化。
    • 生成内镜中心工作量统计表格,并保存为Excel文件。
  3. 医生工作量统计

    • 根据诊断类别和检查类别,统计每个医生的工作量。
    • 生成医生工作量统计表格,并保存为Excel文件。

输出文件:

  • 内镜中心工作量统计.xlsx:包含内镜中心的工作量统计。
  • 医生工作量统计.xlsx:包含每个医生的工作量统计。

注意事项:

  • 代码假设1月和2月的Excel文件结构相同,且数据在名为“导出数据”的工作表中。
  • 如果数据量较大,可能需要优化性能。
  • 确保pandas库已安装(可以通过pip install pandas安装)。

时间: 2025-03-14T00:59:33


👤 用户

Traceback (most recent call last):
File “C:\Users\Notebook\Desktop\script.py”, line 105, in
doctor_stats_table = doctor_stats_table.append({
^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Notebook\AppData\Local\Programs\Python\Python313\Lib\site-packages\pandas\core\generic.py”, line 6299, in getattr
return object.getattribute(self, name)
~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
AttributeError: ‘DataFrame’ object has no attribute ‘append’. Did you mean: ‘_append’?

时间: 2025-03-14T01:05:16


🤖 DeepSeek (deepseek-chat)

在较新版本的 pandas 中,DataFrame.append 方法已被弃用,并推荐使用 pd.concat 来代替。我们需要将代码中的 append 替换为 pd.concat

以下是修正后的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import pandas as pd

# 读取1月和2月的数据
df_jan = pd.read_excel('1.xls', sheet_name='导出数据')
df_feb = pd.read_excel('2.xls', sheet_name='导出数据')

# 定义检查类别的映射规则
def map_category(category):
if category in ['胃镜', '无痛胃镜']:
return ['胃镜检查', '无痛检查']
elif category in ['肠镜', '无痛肠镜']:
return ['肠镜检查', '无痛检查']
elif category == '超声内镜':
return ['超声内镜']
elif category == '十二指肠镜':
return ['十二指肠镜']
else:
return ['其他内镜手术']

# 应用映射规则并展开
def expand_categories(df):
expanded_data = []
for _, row in df.iterrows():
categories = map_category(row['检查类别'])
for cat in categories:
expanded_data.append({**row.to_dict(), '检查类别': cat})
return pd.DataFrame(expanded_data)

# 展开1月和2月的数据
df_jan_expanded = expand_categories(df_jan)
df_feb_expanded = expand_categories(df_feb)

# 统计1月和2月的检查数量
jan_counts = df_jan_expanded['检查类别'].value_counts()
feb_counts = df_feb_expanded['检查类别'].value_counts()

# 计算同比变化
def calculate_change(feb, jan):
if jan == 0:
return 0
return ((feb - jan) / jan) * 100

# 创建内镜中心工作量统计表格
stats = pd.DataFrame({
'项目': ['胃镜检查', '肠镜检查', '无痛检查', '超声内镜', '十二指肠镜', '其他内镜手术', '总检查量'],
'本月数量': [
feb_counts.get('胃镜检查', 0),
feb_counts.get('肠镜检查', 0),
feb_counts.get('无痛检查', 0),
feb_counts.get('超声内镜', 0),
feb_counts.get('十二指肠镜', 0),
feb_counts.get('其他内镜手术', 0),
feb_counts.sum()
],
'上月数量': [
jan_counts.get('胃镜检查', 0),
jan_counts.get('肠镜检查', 0),
jan_counts.get('无痛检查', 0),
jan_counts.get('超声内镜', 0),
jan_counts.get('十二指肠镜', 0),
jan_counts.get('其他内镜手术', 0),
jan_counts.sum()
]
})

stats['同比变化(%)'] = stats.apply(lambda row: calculate_change(row['本月数量'], row['上月数量']), axis=1)

# 保存内镜中心工作量统计表格
stats.to_excel('内镜中心工作量统计.xlsx', index=False)

# 定义医生工作量的统计规则
def map_diagnosis(diagnosis):
if diagnosis is None:
return None
if 'emr' in diagnosis.lower() or 'apc' in diagnosis.lower():
return 'EMR(APC)'
elif 'esd' in diagnosis.lower():
return 'ESD'
elif '止血' in diagnosis:
return '止血术'
elif '扩张' in diagnosis:
return '扩张术'
elif '异物' in diagnosis:
return '异物摄取术'
else:
return None

# 应用诊断映射规则
df_feb['诊断类别'] = df_feb['镜下诊断'].apply(map_diagnosis)

# 统计医生工作量
doctor_stats = df_feb.groupby('报告医师').agg({
'检查类别': lambda x: x.value_counts().to_dict(),
'诊断类别': lambda x: x.value_counts().to_dict()
}).reset_index()

# 创建医生工作量统计表格
doctor_stats_table = pd.DataFrame(columns=['报告医师', '胃镜', '肠镜', '无痛内镜', 'ERCP', 'EMR(APC)', 'ESD', '止血术', '扩张术', '异物摄取术'])

for index, row in doctor_stats.iterrows():
doctor = row['报告医师']
check_counts = row['检查类别']
diagnosis_counts = row['诊断类别']

new_row = pd.DataFrame({
'报告医师': [doctor],
'胃镜': [check_counts.get('胃镜', 0) + check_counts.get('无痛胃镜', 0)],
'肠镜': [check_counts.get('肠镜', 0) + check_counts.get('无痛肠镜', 0)],
'无痛内镜': [check_counts.get('无痛胃镜', 0) + check_counts.get('无痛肠镜', 0)],
'ERCP': [check_counts.get('十二指肠镜', 0)],
'EMR(APC)': [diagnosis_counts.get('EMR(APC)', 0)],
'ESD': [diagnosis_counts.get('ESD', 0)],
'止血术': [diagnosis_counts.get('止血术', 0)],
'扩张术': [diagnosis_counts.get('扩张术', 0)],
'异物摄取术': [diagnosis_counts.get('异物摄取术', 0)]
})
doctor_stats_table = pd.concat([doctor_stats_table, new_row], ignore_index=True)

# 保存医生工作量统计表格
doctor_stats_table.to_excel('医生工作量统计.xlsx', index=False)

主要修改点:

  1. 替换 appendpd.concat

    • 使用 pd.concat 将新行添加到 doctor_stats_table 中。
    • 每次创建一个新的 DataFramenew_row),然后通过 pd.concat 将其合并到 doctor_stats_table 中。
  2. 保持代码逻辑不变

    • 其他部分的逻辑(如检查类别映射、诊断类别映射、同比变化计算等)保持不变。

输出文件:

  • 内镜中心工作量统计.xlsx:包含内镜中心的工作量统计。
  • 医生工作量统计.xlsx:包含每个医生的工作量统计。

注意事项:

  • 确保 pandas 库已安装(可以通过 pip install pandas 安装)。
  • 如果数据量较大,可能需要优化性能(例如使用更高效的数据结构或并行处理)。
  • 代码假设 1 月和 2 月的 Excel 文件结构相同,且数据在名为“导出数据”的工作表中。

时间: 2025-03-14T01:05:16


👤 用户

内镜中心工作量统计表格 中总量不包括无痛内镜数量,因为无痛胃镜和无痛肠镜已经被统计在胃镜和肠镜中了

时间: 2025-03-14T01:32:35


🤖 DeepSeek (deepseek-chat)

明白了!在“内镜中心工作量统计”表格中,总检查量不应包括“无痛检查”的数量,因为“无痛胃镜”和“无痛肠镜”已经被分别统计到“胃镜检查”和“肠镜检查”中了。我们需要调整总检查量的计算逻辑,确保不重复统计。

以下是修正后的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import pandas as pd

# 读取1月和2月的数据
df_jan = pd.read_excel('1.xls', sheet_name='导出数据')
df_feb = pd.read_excel('2.xls', sheet_name='导出数据')

# 定义检查类别的映射规则
def map_category(category):
if category in ['胃镜', '无痛胃镜']:
return ['胃镜检查', '无痛检查']
elif category in ['肠镜', '无痛肠镜']:
return ['肠镜检查', '无痛检查']
elif category == '超声内镜':
return ['超声内镜']
elif category == '十二指肠镜':
return ['十二指肠镜']
else:
return ['其他内镜手术']

# 应用映射规则并展开
def expand_categories(df):
expanded_data = []
for _, row in df.iterrows():
categories = map_category(row['检查类别'])
for cat in categories:
expanded_data.append({**row.to_dict(), '检查类别': cat})
return pd.DataFrame(expanded_data)

# 展开1月和2月的数据
df_jan_expanded = expand_categories(df_jan)
df_feb_expanded = expand_categories(df_feb)

# 统计1月和2月的检查数量
jan_counts = df_jan_expanded['检查类别'].value_counts()
feb_counts = df_feb_expanded['检查类别'].value_counts()

# 计算同比变化
def calculate_change(feb, jan):
if jan == 0:
return 0
return ((feb - jan) / jan) * 100

# 创建内镜中心工作量统计表格
stats = pd.DataFrame({
'项目': ['胃镜检查', '肠镜检查', '无痛检查', '超声内镜', '十二指肠镜', '其他内镜手术', '总检查量'],
'本月数量': [
feb_counts.get('胃镜检查', 0),
feb_counts.get('肠镜检查', 0),
feb_counts.get('无痛检查', 0),
feb_counts.get('超声内镜', 0),
feb_counts.get('十二指肠镜', 0),
feb_counts.get('其他内镜手术', 0),
# 总检查量 = 胃镜检查 + 肠镜检查 + 超声内镜 + 十二指肠镜 + 其他内镜手术
feb_counts.get('胃镜检查', 0) + feb_counts.get('肠镜检查', 0) +
feb_counts.get('超声内镜', 0) + feb_counts.get('十二指肠镜', 0) +
feb_counts.get('其他内镜手术', 0)
],
'上月数量': [
jan_counts.get('胃镜检查', 0),
jan_counts.get('肠镜检查', 0),
jan_counts.get('无痛检查', 0),
jan_counts.get('超声内镜', 0),
jan_counts.get('十二指肠镜', 0),
jan_counts.get('其他内镜手术', 0),
# 总检查量 = 胃镜检查 + 肠镜检查 + 超声内镜 + 十二指肠镜 + 其他内镜手术
jan_counts.get('胃镜检查', 0) + jan_counts.get('肠镜检查', 0) +
jan_counts.get('超声内镜', 0) + jan_counts.get('十二指肠镜', 0) +
jan_counts.get('其他内镜手术', 0)
]
})

# 计算同比变化
stats['同比变化(%)'] = stats.apply(lambda row: calculate_change(row['本月数量'], row['上月数量']), axis=1)

# 保存内镜中心工作量统计表格
stats.to_excel('内镜中心工作量统计.xlsx', index=False)

# 定义医生工作量的统计规则
def map_diagnosis(diagnosis):
if diagnosis is None:
return None
if 'emr' in diagnosis.lower() or 'apc' in diagnosis.lower():
return 'EMR(APC)'
elif 'esd' in diagnosis.lower():
return 'ESD'
elif '止血' in diagnosis:
return '止血术'
elif '扩张' in diagnosis:
return '扩张术'
elif '异物' in diagnosis:
return '异物摄取术'
else:
return None

# 应用诊断映射规则
df_feb['诊断类别'] = df_feb['镜下诊断'].apply(map_diagnosis)

# 统计医生工作量
doctor_stats = df_feb.groupby('报告医师').agg({
'检查类别': lambda x: x.value_counts().to_dict(),
'诊断类别': lambda x: x.value_counts().to_dict()
}).reset_index()

# 创建医生工作量统计表格
doctor_stats_table = pd.DataFrame(columns=['报告医师', '胃镜', '肠镜', '无痛内镜', 'ERCP', 'EMR(APC)', 'ESD', '止血术', '扩张术', '异物摄取术'])

for index, row in doctor_stats.iterrows():
doctor = row['报告医师']
check_counts = row['检查类别']
diagnosis_counts = row['诊断类别']

new_row = pd.DataFrame({
'报告医师': [doctor],
'胃镜': [check_counts.get('胃镜', 0) + check_counts.get('无痛胃镜', 0)],
'肠镜': [check_counts.get('肠镜', 0) + check_counts.get('无痛肠镜', 0)],
'无痛内镜': [check_counts.get('无痛胃镜', 0) + check_counts.get('无痛肠镜', 0)],
'ERCP': [check_counts.get('十二指肠镜', 0)],
'EMR(APC)': [diagnosis_counts.get('EMR(APC)', 0)],
'ESD': [diagnosis_counts.get('ESD', 0)],
'止血术': [diagnosis_counts.get('止血术', 0)],
'扩张术': [diagnosis_counts.get('扩张术', 0)],
'异物摄取术': [diagnosis_counts.get('异物摄取术', 0)]
})
doctor_stats_table = pd.concat([doctor_stats_table, new_row], ignore_index=True)

# 保存医生工作量统计表格
doctor_stats_table.to_excel('医生工作量统计.xlsx', index=False)

主要修改点:

  1. 总检查量的计算

    • 总检查量 = 胃镜检查 + 肠镜检查 + 超声内镜 + 十二指肠镜 + 其他内镜手术。
    • 不包含“无痛检查”,因为“无痛胃镜”和“无痛肠镜”已经被分别统计到“胃镜检查”和“肠镜检查”中。
  2. 其他逻辑保持不变

    • 检查类别映射、诊断类别映射、同比变化计算等逻辑保持不变。

输出文件:

  • 内镜中心工作量统计.xlsx:包含内镜中心的工作量统计。
  • 医生工作量统计.xlsx:包含每个医生的工作量统计。

示例输出:

内镜中心工作量统计:

项目 本月数量 上月数量 同比变化(%)
胃镜检查 630 564 +11.7%
肠镜检查 181 192 -5.7%
无痛检查 181 192 -5.7%
超声内镜 50 45 +11.1%
十二指肠镜 30 25 +20.0%
其他内镜手术 10 8 +25.0%
总检查量 901 834 +8.0%

医生工作量统计:

报告医师 胃镜 肠镜 无痛内镜 ERCP EMR(APC) ESD 止血术 扩张术 异物摄取术
柳鹏飞 30 20 40 2 10 5 3 2 1
钟晓锋 25 15 35 3 8 4 2 1 1
汤瑜 20 10 30 1 5 3 1 1 0

注意事项:

  • 确保 pandas 库已安装(可以通过 pip install pandas 安装)。
  • 如果数据量较大,可能需要优化性能(例如使用更高效的数据结构或并行处理)。
  • 代码假设 1 月和 2 月的 Excel 文件结构相同,且数据在名为“导出数据”的工作表中。

时间: 2025-03-14T01:32:35


👤 用户

同理,在医生工作量统计的时候也应该扣除无痛内镜统计,因为无痛胃镜和无痛肠镜已经被统计进胃镜和肠镜中了。

时间: 2025-03-14T01:47:39


🤖 DeepSeek (deepseek-chat)

明白了!在“医生工作量统计”表格中,无痛内镜的数量也不应单独统计,因为“无痛胃镜”和“无痛肠镜”已经被分别统计到“胃镜”和“肠镜”中了。我们需要调整医生工作量统计的逻辑,确保不重复统计。

以下是修正后的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import pandas as pd

# 读取1月和2月的数据
df_jan = pd.read_excel('1.xls', sheet_name='导出数据')
df_feb = pd.read_excel('2.xls', sheet_name='导出数据')

# 定义检查类别的映射规则
def map_category(category):
if category in ['胃镜', '无痛胃镜']:
return ['胃镜检查', '无痛检查']
elif category in ['肠镜', '无痛肠镜']:
return ['肠镜检查', '无痛检查']
elif category == '超声内镜':
return ['超声内镜']
elif category == '十二指肠镜':
return ['十二指肠镜']
else:
return ['其他内镜手术']

# 应用映射规则并展开
def expand_categories(df):
expanded_data = []
for _, row in df.iterrows():
categories = map_category(row['检查类别'])
for cat in categories:
expanded_data.append({**row.to_dict(), '检查类别': cat})
return pd.DataFrame(expanded_data)

# 展开1月和2月的数据
df_jan_expanded = expand_categories(df_jan)
df_feb_expanded = expand_categories(df_feb)

# 统计1月和2月的检查数量
jan_counts = df_jan_expanded['检查类别'].value_counts()
feb_counts = df_feb_expanded['检查类别'].value_counts()

# 计算同比变化
def calculate_change(feb, jan):
if jan == 0:
return 0
return ((feb - jan) / jan) * 100

# 创建内镜中心工作量统计表格
stats = pd.DataFrame({
'项目': ['胃镜检查', '肠镜检查', '无痛检查', '超声内镜', '十二指肠镜', '其他内镜手术', '总检查量'],
'本月数量': [
feb_counts.get('胃镜检查', 0),
feb_counts.get('肠镜检查', 0),
feb_counts.get('无痛检查', 0),
feb_counts.get('超声内镜', 0),
feb_counts.get('十二指肠镜', 0),
feb_counts.get('其他内镜手术', 0),
# 总检查量 = 胃镜检查 + 肠镜检查 + 超声内镜 + 十二指肠镜 + 其他内镜手术
feb_counts.get('胃镜检查', 0) + feb_counts.get('肠镜检查', 0) +
feb_counts.get('超声内镜', 0) + feb_counts.get('十二指肠镜', 0) +
feb_counts.get('其他内镜手术', 0)
],
'上月数量': [
jan_counts.get('胃镜检查', 0),
jan_counts.get('肠镜检查', 0),
jan_counts.get('无痛检查', 0),
jan_counts.get('超声内镜', 0),
jan_counts.get('十二指肠镜', 0),
jan_counts.get('其他内镜手术', 0),
# 总检查量 = 胃镜检查 + 肠镜检查 + 超声内镜 + 十二指肠镜 + 其他内镜手术
jan_counts.get('胃镜检查', 0) + jan_counts.get('肠镜检查', 0) +
jan_counts.get('超声内镜', 0) + jan_counts.get('十二指肠镜', 0) +
jan_counts.get('其他内镜手术', 0)
]
})

# 计算同比变化
stats['同比变化(%)'] = stats.apply(lambda row: calculate_change(row['本月数量'], row['上月数量']), axis=1)

# 保存内镜中心工作量统计表格
stats.to_excel('内镜中心工作量统计.xlsx', index=False)

# 定义医生工作量的统计规则
def map_diagnosis(diagnosis):
if diagnosis is None:
return None
if 'emr' in diagnosis.lower() or 'apc' in diagnosis.lower():
return 'EMR(APC)'
elif 'esd' in diagnosis.lower():
return 'ESD'
elif '止血' in diagnosis:
return '止血术'
elif '扩张' in diagnosis:
return '扩张术'
elif '异物' in diagnosis:
return '异物摄取术'
else:
return None

# 应用诊断映射规则
df_feb['诊断类别'] = df_feb['镜下诊断'].apply(map_diagnosis)

# 统计医生工作量
doctor_stats = df_feb.groupby('报告医师').agg({
'检查类别': lambda x: x.value_counts().to_dict(),
'诊断类别': lambda x: x.value_counts().to_dict()
}).reset_index()

# 创建医生工作量统计表格
doctor_stats_table = pd.DataFrame(columns=['报告医师', '胃镜', '肠镜', 'ERCP', 'EMR(APC)', 'ESD', '止血术', '扩张术', '异物摄取术'])

for index, row in doctor_stats.iterrows():
doctor = row['报告医师']
check_counts = row['检查类别']
diagnosis_counts = row['诊断类别']

new_row = pd.DataFrame({
'报告医师': [doctor],
'胃镜': [check_counts.get('胃镜', 0) + check_counts.get('无痛胃镜', 0)],
'肠镜': [check_counts.get('肠镜', 0) + check_counts.get('无痛肠镜', 0)],
'ERCP': [check_counts.get('十二指肠镜', 0)],
'EMR(APC)': [diagnosis_counts.get('EMR(APC)', 0)],
'ESD': [diagnosis_counts.get('ESD', 0)],
'止血术': [diagnosis_counts.get('止血术', 0)],
'扩张术': [diagnosis_counts.get('扩张术', 0)],
'异物摄取术': [diagnosis_counts.get('异物摄取术', 0)]
})
doctor_stats_table = pd.concat([doctor_stats_table, new_row], ignore_index=True)

# 保存医生工作量统计表格
doctor_stats_table.to_excel('医生工作量统计.xlsx', index=False)

主要修改点:

  1. 医生工作量统计中移除“无痛内镜”列

    • 因为“无痛胃镜”和“无痛肠镜”已经被分别统计到“胃镜”和“肠镜”中,所以不再单独统计“无痛内镜”。
  2. 其他逻辑保持不变

    • 检查类别映射、诊断类别映射、同比变化计算等逻辑保持不变。

输出文件:

  • 内镜中心工作量统计.xlsx:包含内镜中心的工作量统计。
  • 医生工作量统计.xlsx:包含每个医生的工作量统计。

示例输出:

内镜中心工作量统计:

项目 本月数量 上月数量 同比变化(%)
胃镜检查 630 564 +11.7%
肠镜检查 181 192 -5.7%
无痛检查 181 192 -5.7%
超声内镜 50 45 +11.1%
十二指肠镜 30 25 +20.0%
其他内镜手术 10 8 +25.0%
总检查量 901 834 +8.0%

医生工作量统计:

报告医师 胃镜 肠镜 ERCP EMR(APC) ESD 止血术 扩张术 异物摄取术
柳鹏飞 30 20 2 10 5 3 2 1
钟晓锋 25 15 3 8 4 2 1 1
汤瑜 20 10 1 5 3 1 1 0

注意事项:

  • 确保 pandas 库已安装(可以通过 pip install pandas 安装)。
  • 如果数据量较大,可能需要优化性能(例如使用更高效的数据结构或并行处理)。
  • 代码假设 1 月和 2 月的 Excel 文件结构相同,且数据在名为“导出数据”的工作表中。

时间: 2025-03-14T01:47:39



对话统计

  • 消息总数: 10 条
  • 处理状态: 成功转换