如何比较字典中的值与数据库中的记录?

问题描述 投票:0回答:1

我有一本规则如下的字典:

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 =或

我使用数据库banknote

   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的表。如果规则为true,则键入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 

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

python dictionary rules
1个回答
0
投票

创建字典,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
© www.soinside.com 2019 - 2024. All rights reserved.