Python的Pandas库与Oracle Sql操作的一一对应关系

本文对比了Python的Pandas库与Oracle SQL在数据处理中的常见操作,包括where条件筛选、group by聚合、order by排序、distinct去重、replace替换、累加和累乘、union与union all合并、不同类型的join操作、lead和lag函数以及窗口函数的应用。通过此对照,可以帮助开发者更好地在两者间切换使用。
摘要由CSDN通过智能技术生成

1、where

sql:

select * from temp1
   where position_dt >= 20170704
     and position_dt between 20170701 and 20170704
     and sk_f_prod in ('pd001', 'pd002')
     and secu_type_code not in ('期货', '远期')
     and account_project_name like '证券投资%'
     and (mkt_suspension_info is null or secu_mkt_code = '001')
     and case when prod_name is null then '无名' else prod_name end = '无名'
     )

pandas:

df = df[(df['position_dt'] >= 20170701)
    &(df['position_dt'] <= 20170704)
    &(df.sk_f_prod.isin(['pd001', 'pd002']))
    &(~df.secu_type_code.isin(['期货', '远期']))
    &(df.account_project_name.str.contains('证券投资'))
    &(df.mkt_suspension_info.isnull() | (df.secu_mkt_code == '001'))
    &(df['prod_name'].map((lambda x: x if x else '无名')) == '无名')]

2、group by:求最大值、求和、求数量

sql:

select position_dt,
       sk_f_prod,
       max(locc_position_cost),
       sum(orgc_posi_mkt_val),
       count(case
               when substr(subj_code, 1, 4) = '1102' then  1
               when secu_var = '股票品种' and secu_inner_code like '%600038%' then 0
               else 2
             end) quantity
  from sql_where
 group by position_dt, sk_f_prod

pandas:

df['amount'] = df.apply(lambda x: 1 if x.subj_code[:4] == '1102' else (0 if
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值