参考资料:python 连接oracle -- sqlalchemy及cx_Oracle的使用详解

  oracle指定表缺失值统计 -- 基于cx_Oracle

  1. import pandas as pd
  2. import cx_Oracle as orcl
  3.  
  4. # 批量查询数据缺失率
  5. def missing_count(table_name, where_condition={}, **engine):
  6. #where 条件参数化, str或dict
  7. sql_tab_columns = "select column_name from user_tab_columns \
  8. where table_name = '{}'".format(table_name)
  9.  
  10. db = ConnectOracle(**engine)
  11. #sql_select.encode('utf-8')
  12. columns = db.select_oracle(sql=sql_tab_columns)
  13.  
  14. #生成select语句
  15. ss = ''
  16. for col in columns.COLUMN_NAME:
  17. ss += 'sum(decode({},null, 1, 0)) as {}, '.format(col, col)
  18. ss = ss[:-2]
  19.  
  20. #生成where条件
  21. wh = ''
  22. if where_condition:
  23. wh += ' where '
  24. if type(where_condition)==str:
  25. wh += where_condition
  26. if type(where_condition)==dict:
  27. for key in where_condition.keys():
  28. if type(where_condition[key])!=str:
  29. wh += ('t.' + str(key) + ' = ' +
  30. str(where_condition[key]) + ' and ')
  31. else:
  32. wh += ("t." + str(key) + " = '" +
  33. str(where_condition[key]) + "' and ")
  34. wh = wh[:-4]
  35.  
  36. #print(ss)
  37. sql_select = '''select count(*) as counts, {}
  38. from {} t {}
  39. '''.format(ss, table_name, wh)
  40.  
  41. #print(sql_select)
  42. res = db.select_oracle(sql=sql_select)
  43. return pd.Series(res.values.tolist()[0], index=res.columns)

  缺失值统计2 -- 基于sqlalchemy

  1. import pandas as pd
  2. #import cx_Oracle as orcl
  3. from sqlalchemy import create_engine
  4.  
  5. # 批量查询数据缺失率
  6. def missing_count(table_name, where_condition={}, **config):
  7. #where 条件参数化, str或dict
  8.  
  9. #定义数据库连接
  10. #'oracle://qmcbrt:qmcbrt@10.85.31.20:1521/tqmcbdb'
  11. engine = 'oracle://{username}:{passwd}@{host}:{port}/{sid}'.format(**config) #dbname -- 各版本语法不同
  12. db = create_engine(engine)
  13. #pd.read_sql_query(sql_tab_columns, db)
  14. #db.execute('truncate table {}'.format(ttb))
  15.  
  16. #查询列名 -- 用于生成select项
  17. sql_tab_columns = "select column_name from user_tab_columns where table_name = '{}'".format(table_name)
  18. columns = pd.read_sql_query(sql_tab_columns, db)
  19.  
  20. #生成select项
  21. ss = ''
  22. for col in columns.column_name:
  23. ss += 'sum(decode({}, null, 1, 0)) as {}, '.format(col, col)
  24. ss = ss[:-2]
  25.  
  26. #生成where条件
  27. wh = ''
  28. if where_condition:
  29. wh += ' where '
  30. if type(where_condition)==str:
  31. wh += where_condition
  32. if type(where_condition)==dict:
  33. for key in where_condition.keys():
  34. if type(where_condition[key])!=str:
  35. wh += ('t.' + str(key) + ' = ' +
  36. str(where_condition[key]) + ' and ')
  37. else:
  38. wh += ("t." + str(key) + " = '" +
  39. str(where_condition[key]) + "' and ")
  40. wh = wh[:-4]
  41.  
  42. #select语句
  43. sql_select = '''select count(*) as counts, {} from {} t {} '''.format(ss, table_name, wh)
  44.  
  45. #pd.Series(res.values.tolist()[0], index=res.columns)
  46. res = pd.read_sql_query(sql_select, db)
  47. return res.iloc[0,:]

  示例

  1. config = {
  2. 'username':'qmcb',
  3. 'passwd':'qmcb',
  4. 'host':'localhost',
  5. 'port':'1521',
  6. 'sid':'tqmcbdb'
  7. }
  8. where_condition = {
  9. 'is_normal': 1,
  10. 'is_below_16': 0,
  11. 'is_xs': 0,
  12. 'is_cj': 0,
  13. 'is_dead': 0,
  14. 'AAE138_is_not_null': 0,
  15. 'is_dc': 0,
  16. 'is_px': 0
  17. }
  18. # 计算 QMCB_KM_2019_1_31_1 表的数据缺失数
  19. 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. 四:DRF项目开发的准备

    一: 虚拟环境virtualenv 如果在一台电脑上, 想开发多个不同的项目, 需要用到同一个包的不同版本, 如果使用上面的命令, 在同一个目录下安装或者更新, 新版本会覆盖以前的版本, 其它的项目就 ...

  2. 第 8 章 容器网络 - 071 - 如何定制 Calico 的 IP 池?

    定制IP池 首先定义一个 IP Pool,比如: calicoctl create -f ipPool.yml 用此 IP Pool 创建 calico 网络. docker network crea ...

  3. [JavaScript] 给input标签传值

    body: <input type="text" style="width: 240px;" name="orgname" id=&q ...

  4. 《Practical Vim》第八章:利用动作命令在文档中移动

    面向单词的移动 定义: Vim 提供了面向单词的动作命令,用于将光标正向/反向移动一个单词; 功能 命令 正向移动到下一单词开头 w 反向移动到上一单词的开头 b 正向移动到下一单词(当前单词)的结尾 ...

  5. 使用pandas的部分问题汇总

    pandas(我所用版本0.17)是一个强大数据处理库,在开发金融类系统中我应用到了pandas.Dataframe数据类型,它的数据结构类似一张图表(如下图所示),左边一列为index既行的索引: ...

  6. You Dream. We Test.

      https://www.keysight.com/us/en/home.html https://www.ixiacom.com/ IXIA 被收,才发现她

  7. 『Python CoolBook』数据结构和算法_多变量赋值&“*”的两种用法

    多变量赋值 a = [1,2,(3,4)] b,c,d = a print(b,c,d) b,c,(d,e) = a print(b,c,d,e) 1 2 (3, 4) 1 2 3 4 a = &qu ...

  8. 『Python CoolBook』C扩展库_其六_线程

    GIL操作 想让C扩展代码和Python解释器中的其他进程一起正确的执行, 那么你就需要去释放并重新获取全局解释器锁(GIL). 在Python接口封装中去释放并重新获取全局解释器锁(GIL),此时本 ...

  9. 理解TCP序列号(Sequence Number)和确认号(Acknowledgment Number)

    原文见:http://packetlife.net/blog/2010/jun/7/understanding-tcp-sequence-acknowledgment-numbers/ from:ht ...

  10. Wireshark 过滤 基本语法

    转载 1.过滤IP,如来源IP或者目标IP等于某个IP   例子: ip.src eq 192.168.1.107 or ip.dst eq 192.168.1.107 或者 ip.addr eq 1 ...