首页 > 解决方案 > 如何将字典中的值与数据库中的记录进行比较?

问题描述

我有一本带有这样规则的字典:

dict_items([(8, 'curtosis > -5.274 V entropy > -5.414 V skewness > 4.875 V variance = [[1.2572]]'), (9, 'curtosis > 8.682 V entropy > -4.492 V  skewness > 4.875 V variance = [[0.89512]]')])

其中 V = 或

我使用数据库钞票

   variance  skewness  curtosis  entropy  class
0   3.62160    8.6661   -2.8073 -0.44699      0
1   4.54590    8.1674   -2.4586 -1.46210      0
2   3.86600   -2.6383    1.9242  0.10645      0
3   3.45660    9.5228   -4.0112 -3.59440      0
4   0.32924   -4.4552    4.5718 -0.98880      0
(1372, 5)

我需要将字典中的值与数据库中的每条记录进行比较。例子:

规则 8:

if -2.8073 > -5.274 or -0.44699 > -5.414 or 8.6661 > 4.875 or 3.62160 = 1.2572

然后我创建一个值为 1 或 0 的表。如果规则为真,则输入 1,否则输入 0。类似这样:

    Rule 8  Rule 9  class
0     1      1        0
1     1      0        0
2     1      0        0
3     1      1        0 
4     1      0        0 

我不知道该怎么做。你能帮助我吗?

标签: pythondictionaryrules

解决方案


创建一个字典,diction映射构成规则的值。然后根据您的条件创建列“Rule-8”和“Rule-9”

diction={'Rule-8':[-5.274,-5.414,4.875,1.2572],'Rule-9':[8.682,-4.492, 4.875 ,0.89512]}

df['Rule-8']= ((df['variance']>diction['Rule-8'][0]) & (df['skewness']>diction['Rule-8'][1]) & (df['curtosis']>diction['Rule-8'][2]) & (df['entropy']>diction['Rule-8'][3]))
df['Rule-9']= ((df['variance']>diction['Rule-9'][0]) & (df['skewness']>diction['Rule-9'][1]) & (df['curtosis']>diction['Rule-9'][2]) & (df['entropy']>diction['Rule-9'][3])) 

df['Rule-8']=df['Rule-8'].astype(int)# converts False to 0's and True to 1's
df['Rule-9']=df['Rule-9'].astype(int)# converts False to 0's and True to 1's

推荐阅读