是否可以通过python中的pandas或Sql Queries之类的东西编写SQL查询?

问题描述 投票:0回答:2

我有这个问题:

SELECT count(PROVIDER_ID)  NumOfProviders,SUM(GROSS_AMT) TotalPaid,SERVICE_TYPE
FROM [FD_Stage].[dbo].[tbl_claim_services]
where SERVICE_TYPE is not null
group by SERVICE_TYPE

我可以在熊猫中做这样的事情

python sql-server pandas
2个回答
0
投票

我已经找到了一种方法来做这个,所以在拥有我的数据框并具有一些属性,如“MEMBER_CODE”,“sdl_code”,“PROVIDER_ID”,“GROSS_AMT”我会做下一步

df= rn.read_sql(sql,conn)
df_groupBy= df.groupby(['MEMBER_CODE','sdl_code','PortalDays']).agg({'PROVIDER_ID':'count','GROSS_AMT':'sum'})

希望这可以帮助任何人


0
投票

你可以使用pandassql

>>> import pandas as pd
>>> from pandasql import sqldf, load_meat, load_births
>>> pysqldf = lambda q: sqldf(q, globals())
>>> meat = load_meat()
>>> births = load_births()
>>> print pysqldf("SELECT * FROM meat LIMIT 10;").head()
   date  beef  veal  pork  lamb_and_mutton broilers other_chicken turkey

0  1944-01-01 00:00:00   751    85  1280               89     None          None   None
1  1944-02-01 00:00:00   713    77  1169               72     None          None   None
2  1944-03-01 00:00:00   741    90  1128               75     None          None   None
3  1944-04-01 00:00:00   650    89   978               66     None          None   None
4  1944-05-01 00:00:00   681   106  1029               78     None          None   None

Github Repo:https://github.com/yhat/pandasql

© www.soinside.com 2019 - 2024. All rights reserved.