python 连接 oracle 统计指定表格所有字段的缺失值数
参考资料:python 连接oracle -- sqlalchemy及cx_Oracle的使用详解
oracle指定表缺失值统计 -- 基于cx_Oracle
import pandas as pd
import cx_Oracle as orcl # 批量查询数据缺失率
def missing_count(table_name, where_condition={}, **engine):
#where 条件参数化, str或dict
sql_tab_columns = "select column_name from user_tab_columns \
where table_name = '{}'".format(table_name) db = ConnectOracle(**engine)
#sql_select.encode('utf-8')
columns = db.select_oracle(sql=sql_tab_columns) #生成select语句
ss = ''
for col in columns.COLUMN_NAME:
ss += 'sum(decode({},null, 1, 0)) as {}, '.format(col, col)
ss = ss[:-2] #生成where条件
wh = ''
if where_condition:
wh += ' where '
if type(where_condition)==str:
wh += where_condition
if type(where_condition)==dict:
for key in where_condition.keys():
if type(where_condition[key])!=str:
wh += ('t.' + str(key) + ' = ' +
str(where_condition[key]) + ' and ')
else:
wh += ("t." + str(key) + " = '" +
str(where_condition[key]) + "' and ")
wh = wh[:-4] #print(ss)
sql_select = '''select count(*) as counts, {}
from {} t {}
'''.format(ss, table_name, wh) #print(sql_select)
res = db.select_oracle(sql=sql_select)
return pd.Series(res.values.tolist()[0], index=res.columns)
缺失值统计2 -- 基于sqlalchemy
import pandas as pd
#import cx_Oracle as orcl
from sqlalchemy import create_engine # 批量查询数据缺失率
def missing_count(table_name, where_condition={}, **config):
#where 条件参数化, str或dict #定义数据库连接
#'oracle://qmcbrt:qmcbrt@10.85.31.20:1521/tqmcbdb'
engine = 'oracle://{username}:{passwd}@{host}:{port}/{sid}'.format(**config) #dbname -- 各版本语法不同
db = create_engine(engine)
#pd.read_sql_query(sql_tab_columns, db)
#db.execute('truncate table {}'.format(ttb)) #查询列名 -- 用于生成select项
sql_tab_columns = "select column_name from user_tab_columns where table_name = '{}'".format(table_name)
columns = pd.read_sql_query(sql_tab_columns, db) #生成select项
ss = ''
for col in columns.column_name:
ss += 'sum(decode({}, null, 1, 0)) as {}, '.format(col, col)
ss = ss[:-2] #生成where条件
wh = ''
if where_condition:
wh += ' where '
if type(where_condition)==str:
wh += where_condition
if type(where_condition)==dict:
for key in where_condition.keys():
if type(where_condition[key])!=str:
wh += ('t.' + str(key) + ' = ' +
str(where_condition[key]) + ' and ')
else:
wh += ("t." + str(key) + " = '" +
str(where_condition[key]) + "' and ")
wh = wh[:-4] #select语句
sql_select = '''select count(*) as counts, {} from {} t {} '''.format(ss, table_name, wh) #pd.Series(res.values.tolist()[0], index=res.columns)
res = pd.read_sql_query(sql_select, db)
return res.iloc[0,:]
示例
config = {
'username':'qmcb',
'passwd':'qmcb',
'host':'localhost',
'port':'1521',
'sid':'tqmcbdb'
}
where_condition = {
'is_normal': 1,
'is_below_16': 0,
'is_xs': 0,
'is_cj': 0,
'is_dead': 0,
'AAE138_is_not_null': 0,
'is_dc': 0,
'is_px': 0
}
# 计算 QMCB_KM_2019_1_31_1 表的数据缺失数
missing_count('QMCB_KM_2019_1_31_1', where_condition, **config)
python 连接 oracle 统计指定表格所有字段的缺失值数的更多相关文章
- python 连接oracle -- sqlalchemy及cx_Oracle的使用详解
python连接oracle -- sqlalchemy import cx_Oracle as orcl import pandas as pd from sqlalchemy import cre ...
- python 连接 Oracle 乱码问题(cx_Oracle)
用python连接Oracle是总是乱码,最后发现时oracle客户端的字符编码设置不对. 编写的python脚本中需要加入如下几句: import os os.environ['NLS_LANG'] ...
- python连接Oracle的方式以及过程中遇到的问题
一.库连接步骤 1.下载cx_Oracle模块 下载步骤 工具 pycharm :File--->右键setting--->找到Project Interpreter -----> ...
- Python连接Oracle数据查询导出结果
python连接oracle,需用用到模块cx_oracle,可以直接pip安装,如网络不好,可下载离线后本地安装 cx_oracle项目地址:https://pypi.org/project/cx_ ...
- Python 连接 Oracle数据库
1.环境设置 [root@oracle ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@oracle ~]# python - ...
- Python 连接Oracle数据库
连接:python操作oracle数据库 python——连接Oracle数据库 python模块:cx_Oracle, DBUtil 大概步骤: 1. 下载模块 cx_Oracle (注意版本) ...
- Python连接oracle数据库 例子一
step1:下载cx_Oracle模块,cmd--pip install cx_Oracle step2: 1 import cx_Oracle #引用模块cx_Oracle 2 conn=cx_Or ...
- python连接oracle导出数据文件
python连接oracle,感觉table_list文件内的表名,来卸载数据文件 主脚本: import os import logging import sys import configpars ...
- Python连接Oracle问题
Python连接Oracle问题 1.pip install cx_oracle 2.会出现乱码问题: 方法一:配置环境变量 export NLS_LANG="SIMPLIF ...
随机推荐
- js +与?优先级
var val = 'smtg'; console.log('Value is ' + (val === 'smtg') ? 'Something' : 'Nothing'); // A. Value ...
- Linux下修改用户的UID、GID
01.用户的UID和GID不能被占用 [root@26 ~]# id mvpuid=503(mvp) gid=503(mvp) groups=503(mvp) ###假定我需要设置mvp的uid/gi ...
- HDU 2612 (2次BFS,有点小细节)
Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...
- 机器学习之决策树_CART算法
目录 3.CART算法(classification and regression tree tree) 3.1 CART生成算法(回归树生成和分类树生成) 3.2 CART剪枝 决策树基本知识参考, ...
- 从Excel、CSV文件获取数据
#region 从Excel获取数据 /// <summary> /// 从Excel获取数据 /// </summary> /// <param name=" ...
- Python---高级函数map, filter, zip, enumerate等的用法
今天看自然语言处理这本书的时候,被这里的高级函数的概念吸引了,因为我觉得所有的函数都只是函数而已,是为了实现特定功能而实现的,不应该有高级,低级之分啊!不过了解之后,发现这几个函数确实是有点高级,非常 ...
- javascript 位操作符
not: 按位非,符号为波浪线~ 作用吧二进制数的所有位进行非操作,对应的十进制结果为原先10进制数字取负值然后减去1 其他的操作符感觉不是很常用,分别为按位与(&),按位或(|),左移(&l ...
- vs2017如何设置类或函数前不显示引用的数量
这几天,从vs2013换成vs2017,17版本增加了一个类或函数前提示引用的数量,这个感觉很别扭,如何取消显示这个呢? 问题如下: 取消显示这个引用的步骤: 找到菜单栏: 工具 ---> 选项 ...
- 查看apk文件包名的一些方法
1,如果有源码 直接将apk包修改为zip,并解压找到AndroidManifest.xml文件,在文件中搜索“package”找到相应的包名 2,使用adb命令 前提是已经下载android SDK ...
- Vuejs的$watch实现原理
大概原理如下面代码所示: class Vue { //Vue对象 constructor (options) { this.$options=options; let data = this._dat ...