You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.2 KiB
Python

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import pandas as pd
import numpy as np
# 读取Excel文件
df = pd.read_excel('data/data_src.xlsx')
# 提取10个因子的列数据
factor_columns = ['躯体化','强迫症状','人际关系敏感','抑郁','焦虑','敌对','恐怖','偏执','精神病性','其他']
factors_data = df[factor_columns]
#factors_data = df.iloc[:, 34:44]
# 计算每行中因子分值大于等于3的数量
factors_above_3 = factors_data.ge(3).sum(axis=1)
# print(factors_above_3)
# input()
# 计算精神病性因子分值大于等于4的数量
psychotic_above_4 = df['精神病性'].ge(4)
# 划分等级类别
df['Level'] = None
df.loc[(factors_above_3 >= 8) | psychotic_above_4, 'Level'] = 1
df.loc[(factors_above_3 >= 1) & (factors_above_3 <= 7), 'Level'] = 2
df.loc[(factors_above_3 >= 1) & (factors_above_3 < 3), 'Level'] = 3
df.loc[factors_above_3 < 1, 'Level'] = 4
# 选择需要保留的列数据
selected_columns = ['编号']+factor_columns + ['Level']
selected_data = df[selected_columns]
# 重置行索引确保顺序与之前的Excel一致
#selected_data = selected_data.reset_index(drop=True)
selected_data = selected_data.sort_index()
# 写入新的Excel文件
selected_data.to_excel('output_excel_file.xlsx', index=False)