参考资料: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. Android hide the app icon but show the icon most left

    ActionBar actionBar = getActionBar(); actionBar.setIcon(new ColorDrawable(getResources().getColor(an ...

  2. Java操作MongoDB:连接&增&删&改&查

    1.连接 ①方式一 MongoClientOptions.Builder builder = MongoClientOptions.builder(); //可以通过builder做各种详细配置 Mo ...

  3. 雷林鹏分享:jQuery EasyUI 数据网格 - 扩展行显示细节

    数据网格(datagrid)可以改变它的视图(view)来显示不同的效果.使用详细视图,数据网格(datagrid)可以在数据行的左边显示展开按钮("+" 或者 "-&q ...

  4. linux存储管理之交换分区

    交换分区管理 Swap ====================================================================================作用: ...

  5. web客户端与服务器端二进制传输

    demo:https://pan.baidu.com/s/1gfEiJ1D 前端采用js,后端采用.net 主要实现js和服务器端之间用二进制通信

  6. CSS中的伪类与伪元素

    在前端开发中,大家或多或少的有接触过CSS伪类和伪元素,使用伪元素的时候,总感觉和伪类很相似,但又不能详细的说出两者的区别和联系,那么两者到底有什么区别和联系呢? 在 W3C 中定义: 伪类:用于向某 ...

  7. ThinkPHP5.0源码学习之执行应用

    一.应用启动 在/thinkphp/start.php文件中,用一句代码App::run()->send();实现应用的启动. // 执行应用 App::run()->send();   ...

  8. 匿名函数lambda及面试题三道

    # 函数名 = lambda 参数 :返回值. 匿名函数只是函数,如果要通过可迭代对象给匿名函数传参,就需要使用 map 或者 filter calc = lambda n:n**n calc(1)c ...

  9. Python中的装饰器的简单介绍02

    这篇博文转载自伯乐在线的12步轻松搞定python装饰器,重构成python3. 1. 函数 在python中,函数通过def关键字.函数名和可选的参数列表定义.通过return关键字返回值.我们举例 ...

  10. EasyExcel导入工具(SpringMVC下使用)

    easyExcel:由阿里巴巴公司开发,由github托管 github上有详细使用文档 github地址:https://github.com/alibaba/easyexcel/blob/mast ...