【问题标题】:How to compare the value from the dictionary with records in the database?如何将字典中的值与数据库中的记录进行比较?
【发布时间】:2020-03-08 16:34:49
【问题描述】:

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

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

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

【问题讨论】:

  • 嗨,我知道你是新来的。如果您认为某个答案解决了问题,请单击绿色复选标记将其标记为“已接受”。这有助于将注意力集中在仍然没有答案的旧 SO。

标签: python dictionary rules


【解决方案1】:

创建一个字典,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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    相关资源
    最近更新 更多