Python 3.x 基于Python中的两个数据帧计算加权总分
我在此excel文件中有两个表格,您可以在其中看到两张表格: 第1页: 第2页: 现在我需要根据Python 3.x 基于Python中的两个数据帧计算加权总分,python-3.x,pandas,dataframe,binary-tree,Python 3.x,Pandas,Dataframe,Binary Tree,我在此excel文件中有两个表格,您可以在其中看到两张表格: 第1页: 第2页: 现在我需要根据Input表中的Input值计算总分,计算逻辑如下:如果输入值为1,则根据该项目的Level score*Weight计算,如果为0,则忽略这些项目。最后,我们需要将加权分数相加得到总分 我们如何在Pandas或其他Python包中做到这一点 谢谢。根据我的理解,您可以提取字段 表1(数据框1)->级别分数和权重 表2(数据框2)->输入值 并将两个数据帧合并到一个数据帧中,现在可以使用新的数据帧
Input表中的Input
值计算总分
,计算逻辑如下:如果输入值为1
,则根据该项目的Level score*Weight
计算,如果为0
,则忽略这些项目。最后,我们需要将加权分数相加得到总分
我们如何在Pandas或其他Python包中做到这一点
谢谢。根据我的理解,您可以提取字段
表1(数据框1)->级别分数和权重
表2(数据框2)->输入值
并将两个数据帧合并到一个数据帧中,现在可以使用新的数据帧执行操作
如果您也需要相同的代码,请告诉我。根据我的理解,您可以提取字段
表1(数据框1)->级别分数和权重
表2(数据框2)->输入值
并将两个数据帧合并到一个数据帧中,现在可以使用新的数据帧执行操作
如果您还需要相同的代码,请告诉我。您可以使用以下工具阅读excel:
input_df = pd.read_excel('../../../../Downloads/test.xlsx', sheet_name='input').fillna(method='ffill')
valuate_df = pd.read_excel('../../../../Downloads/test.xlsx', sheet_name='valuate').fillna(method='ffill')
然后,您应该将它们合并到一个表中。如果您确定它们的顺序相同,请使用以下命令:
df = input_df.join(valuate_df[['Weight', 'Level score']])
它从评估表中选择两列['Weight','levelscore'],并将它们连接到第一个表。您将拥有如下所示的新表:
Evaluation type Evaluation module Evaluation index Unnamed: 3 Evaluation content Division level Input Weight Level score
0 Business application security User Management Ordinary account authentication NaN Evaluation item 1 Level 3 0 0.010490 3
1 Business application security User Management Ordinary account authentication NaN Evaluation item 2 Level 3 1 0.010490 3
2 Business application security User Management Ordinary account authentication NaN Evaluation item 3 Level 3 1 0.010490 3
3 Business application security User Management Public account identity verification NaN Evaluation item 1 Second level 0 0.006993 2
4 Business platform security Business platform deployment Platform equipment information record NaN Evaluation item 1 Second level 1 0.006993 2
5 Business platform security Business platform deployment Facility distribution at home and abroad NaN Evaluation item 1 Level 3 1 0.010490 3
6 Business platform security Resource Scheduling Real-time resource monitoring NaN Evaluation item 1 Second level 0 0.006993 2
7 Business platform security Resource Scheduling Detection and processing of illegal information NaN Evaluation item 1 Second level 0 0.006993 2
8 Business platform security Resource Scheduling System log retention NaN Evaluation item 1 Level 3 0 0.010490 3
现在,您可以选择输入不是0的行:
df = df[df.Input!=0]
然后计算总分:
total_score = sum(df['Level score'] * df['Weight'])
因此,本例的总分为0.1083915
,是吗?您可以使用以下工具阅读excel:
input_df = pd.read_excel('../../../../Downloads/test.xlsx', sheet_name='input').fillna(method='ffill')
valuate_df = pd.read_excel('../../../../Downloads/test.xlsx', sheet_name='valuate').fillna(method='ffill')
然后,您应该将它们合并到一个表中。如果您确定它们的顺序相同,请使用以下命令:
df = input_df.join(valuate_df[['Weight', 'Level score']])
它从评估表中选择两列['Weight','levelscore'],并将它们连接到第一个表。您将拥有如下所示的新表:
Evaluation type Evaluation module Evaluation index Unnamed: 3 Evaluation content Division level Input Weight Level score
0 Business application security User Management Ordinary account authentication NaN Evaluation item 1 Level 3 0 0.010490 3
1 Business application security User Management Ordinary account authentication NaN Evaluation item 2 Level 3 1 0.010490 3
2 Business application security User Management Ordinary account authentication NaN Evaluation item 3 Level 3 1 0.010490 3
3 Business application security User Management Public account identity verification NaN Evaluation item 1 Second level 0 0.006993 2
4 Business platform security Business platform deployment Platform equipment information record NaN Evaluation item 1 Second level 1 0.006993 2
5 Business platform security Business platform deployment Facility distribution at home and abroad NaN Evaluation item 1 Level 3 1 0.010490 3
6 Business platform security Resource Scheduling Real-time resource monitoring NaN Evaluation item 1 Second level 0 0.006993 2
7 Business platform security Resource Scheduling Detection and processing of illegal information NaN Evaluation item 1 Second level 0 0.006993 2
8 Business platform security Resource Scheduling System log retention NaN Evaluation item 1 Level 3 0 0.010490 3
现在,您可以选择输入不是0的行:
df = df[df.Input!=0]
然后计算总分:
total_score = sum(df['Level score'] * df['Weight'])
所以这个例子的总分是0.1083915
,是吗