python - 如何将字典中的值与数据库中的记录进行比较?
问题描述
我有一本带有这样规则的字典:
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
我不知道该怎么做。你能帮助我吗?
解决方案
创建一个字典,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
推荐阅读
- c++ - 为什么 std::shared_ptr 被认为是“沉重的”和“昂贵的”,但 std::array “与普通(c 风格)数组的性能相同”?
- javascript - 如何从 7 个字符显示 4 个字符进行排列?
- r - R gtsummary 包:如何在汇总表中操作/隐藏行
- razor - 如何在不破坏 C# 模板的情况下拥有可选的日期/时间选择器?
- python - “额外的参数必须在一个元组中”是什么意思?
- laravel - Laravel Passport 机器对机器 API 认证
- mysql - mysql root用户没有权限
- python - 我无法在薪金税计算器中计算税金函数
- javascript - 为什么 JavaScript DOM 颜色操作不起作用?
- mysql - 具有多列的 MySQL WHERE IN 子查询