参考资料: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 统计指定表格所有字段的缺失值数的更多相关文章

  1. python 连接oracle -- sqlalchemy及cx_Oracle的使用详解

    python连接oracle -- sqlalchemy import cx_Oracle as orcl import pandas as pd from sqlalchemy import cre ...

  2. python 连接 Oracle 乱码问题(cx_Oracle)

    用python连接Oracle是总是乱码,最后发现时oracle客户端的字符编码设置不对. 编写的python脚本中需要加入如下几句: import os os.environ['NLS_LANG'] ...

  3. python连接Oracle的方式以及过程中遇到的问题

    一.库连接步骤 1.下载cx_Oracle模块 下载步骤 工具 pycharm :File--->右键setting--->找到Project Interpreter  -----> ...

  4. Python连接Oracle数据查询导出结果

    python连接oracle,需用用到模块cx_oracle,可以直接pip安装,如网络不好,可下载离线后本地安装 cx_oracle项目地址:https://pypi.org/project/cx_ ...

  5. Python 连接 Oracle数据库

    1.环境设置 [root@oracle ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@oracle ~]# python - ...

  6. Python 连接Oracle数据库

    连接:python操作oracle数据库  python——连接Oracle数据库 python模块:cx_Oracle, DBUtil 大概步骤: 1. 下载模块 cx_Oracle (注意版本) ...

  7. Python连接oracle数据库 例子一

    step1:下载cx_Oracle模块,cmd--pip install cx_Oracle step2: 1 import cx_Oracle #引用模块cx_Oracle 2 conn=cx_Or ...

  8. python连接oracle导出数据文件

    python连接oracle,感觉table_list文件内的表名,来卸载数据文件 主脚本: import os import logging import sys import configpars ...

  9. Python连接Oracle问题

    Python连接Oracle问题 1.pip install cx_oracle 2.会出现乱码问题:     方法一:配置环境变量     export NLS_LANG="SIMPLIF ...

随机推荐

  1. Hbasewindows系统下启动报错及解决办法

    今天在本地windows电脑上,装pinpoint时,需要先安装一个Hbase数据库,按照教程下载启动Hbase数据库时,却启动报错:java.io.IOException: Could not lo ...

  2. 省市区三级联选select2.js

    <div class="mui-input-row row_then" id='showCityPicker3'> <input id='cityResult3' ...

  3. 20175317 《Java程序设计》第三周学习总结

    20175317 <Java程序设计>第三周学习总结 教材学习内容总结 第三周我学习了教材第四章的内容,了解了Java中的部分常用语句,学到了以下内容: 明白了什么是类,成员变量有哪些,什 ...

  4. 『TensorFlow』slim模块常用API

    辅助函数 slim.arg_scope() slim.arg_scope可以定义一些函数的默认参数值,在scope内,我们重复用到这些函数时可以不用把所有参数都写一遍,注意它没有tf.variable ...

  5. 【转载】GET和POST两种基本请求方法的区别

    原文地址:http://www.cnblogs.com/logsharing/p/8448446.html GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一 ...

  6. 对聊天室项目的NABCD的分析

    NABCD需求分析: 需求(N):我们的项目是制作一个局域网内的聊天室软件,为了解决一个公司或者小团体内小范围的局域的简单通讯问题,我们针对的需求是简单与安全. 做法(A):用Java来实现一个C/S ...

  7. NSIS脚本 打包安装程序

    相关工具 nsis http://nsis.sourceforge.net/Special_Builds HM NIS Edit http://hmne.sourceforge.net/ 例子: ; ...

  8. TensorFlow中assign函数

    tf.assign assign ( ref , value , validate_shape = None , use_locking = None , name = None ) 定义在:tens ...

  9. MySQL:数据操作

    数据操作 一.插入数据 1.所有字段插入数据 语法: insert into 数据表名(字段名) values(插入的数据);(标准)insert into 数据表名values(插入的数据); 实例 ...

  10. binary and out mode to open a file

    When I use binary and out mode to open a exist file, and to modify the 4th and 8th byte data to 0x78 ...