Files
AIclinicalresearch/python-microservice/operations/recode.py
HaHafeng 5f089516cb feat(iit-manager): Day 3 企业微信集成开发完成
- 新增WechatService(企业微信推送服务,支持文本/卡片/Markdown消息)
- 新增WechatCallbackController(异步回复模式,5秒内响应)
- 完善iit_quality_check Worker(调用WechatService推送通知)
- 新增企业微信回调路由(GET验证+POST接收消息)
- 实现LLM意图识别(query_weekly_summary/query_patient_info等)
- 安装依赖:@wecom/crypto, xml2js
- 更新开发记录文档和MVP开发计划

技术要点:
- 使用异步回复模式规避企业微信5秒超时限制
- 使用@wecom/crypto官方库处理XML加解密
- 使用setImmediate实现后台异步处理
- 支持主动推送消息返回LLM处理结果
- 完善审计日志记录(WECHAT_NOTIFICATION_SENT/WECHAT_INTERACTION)

相关文档:
- docs/03-业务模块/IIT Manager Agent/06-开发记录/Day3-企业微信集成开发完成记录.md
- docs/03-业务模块/IIT Manager Agent/04-开发计划/最小MVP闭环开发计划.md
- docs/03-业务模块/IIT Manager Agent/00-模块当前状态与开发指南.md
2026-01-03 09:39:39 +08:00

112 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
数值映射(重编码)操作
将分类变量的原始值映射为新值男→1女→2
"""
import pandas as pd
from typing import Dict, Any, Optional
def apply_recode(
df: pd.DataFrame,
column: str,
mapping: Dict[Any, Any],
create_new_column: bool = True,
new_column_name: Optional[str] = None
) -> pd.DataFrame:
"""
应用数值映射
Args:
df: 输入数据框
column: 要重编码的列名
mapping: 映射字典,如 {'': 1, '': 2}
create_new_column: 是否创建新列True或覆盖原列False
new_column_name: 新列名create_new_column=True时使用
Returns:
重编码后的数据框
Examples:
>>> df = pd.DataFrame({'性别': ['', '', '', '']})
>>> mapping = {'': 1, '': 2}
>>> result = apply_recode(df, '性别', mapping, True, '性别_编码')
>>> result['性别_编码'].tolist()
[1, 2, 1, 2]
"""
if df.empty:
return df
# 验证列是否存在
if column not in df.columns:
raise KeyError(f"'{column}' 不存在")
if not mapping:
raise ValueError('映射字典不能为空')
# 确定目标列名
if create_new_column:
target_column = new_column_name or f'{column}_编码'
else:
target_column = column
# 创建结果数据框(避免修改原数据)
result = df.copy()
# 应用映射
result[target_column] = result[column].map(mapping)
# 统计结果
mapped_count = result[target_column].notna().sum()
unmapped_count = result[target_column].isna().sum()
total_count = len(result)
print(f'映射完成: {mapped_count} 个值成功映射')
if unmapped_count > 0:
print(f'警告: {unmapped_count} 个值未找到对应映射')
# 找出未映射的唯一值
unmapped_mask = result[target_column].isna()
unmapped_values = result.loc[unmapped_mask, column].unique()
print(f'未映射的值: {list(unmapped_values)[:10]}') # 最多显示10个
# 映射成功率
success_rate = (mapped_count / total_count * 100) if total_count > 0 else 0
print(f'映射成功率: {success_rate:.1f}%')
return result