Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将SQL的groupby、select、count(*)和where命令一起使用_Python_Sql_Pandas - Fatal编程技术网

Python 如何将SQL的groupby、select、count(*)和where命令一起使用

Python 如何将SQL的groupby、select、count(*)和where命令一起使用,python,sql,pandas,Python,Sql,Pandas,我不熟悉用python编写SQL查询 我有这样一个SQL查询 select Category, Date, count(*) as Uniq, sum(FCnt) as Total, sum(FCnt)/count(*) as RepRatio, Mod,Act,Exp, Sel, Bias,Sel_B,Bias_B,Bias_P,Con_Num, Sel_Str,CG_D,CGM,TC,P_Value from FCntt_Table where Sel_B=Bias_B group

我不熟悉用python编写SQL查询

我有这样一个SQL查询

 select Category, Date, count(*) as Uniq, sum(FCnt) as Total,
 sum(FCnt)/count(*) as RepRatio, Mod,Act,Exp, Sel,
 Bias,Sel_B,Bias_B,Bias_P,Con_Num, Sel_Str,CG_D,CGM,TC,P_Value from
 FCntt_Table where Sel_B=Bias_B group by
 Mod,Act,Exp,Bias_P,Con_Num,CG_D order by RepRatio desc, Uniq desc;
我试图将此查询转换为python代码,以便使用python执行此查询所完成的操作。我遇到了使用熊猫的选项

我将SQL表设置为.csv格式

我写的代码是

import pandas as pd
import numpy as np

tips=pd.read_csv("fc.csv")


tips["Total"]=tips.groupby(['Mod','Act','Exp','Bias_P','Con_Num','CG_D'])["FCnt"].transform("sum")

tips[tips['Sel_B'] == tips['Bias_B']]
print tips.groupby(['Mod','Act','Exp','Bias_P','Con_Num','CG_D']).agg({'Uniq':np.size})

print tips.head(5)
但这给了我Uniq的错误。请帮我输入这个代码

样本数据:(由OP在评论中提供)


由于您想要获取组的总和和计数,我以不同的方式使用了聚合函数,即,
.agg({'FCnt':(np.sum,np.size)}

代码:

tips=pd.read_clipboard(sep=',')
# filtered_tips = tips[tips['Sel_B'] == tips['Bias_B']] # In given sample data, there is zero records after filter.

# So, considering original df
tips.groupby(['Mod','Act','Exp','Bias_P','Con_Num','CG_D']).agg({'FCnt':(np.sum,np.size)})
tips.columns = ['Total', 'Count']
print(group_df.reset_index())

   Mod  Act  Exp  Bias_P Con_Num CG_D  Total  Count
0  649    0  GP2       0     SB3  cg1     13      3
1  649    0  GP2       0     SB3  cg2      1      1

输出:

tips=pd.read_clipboard(sep=',')
# filtered_tips = tips[tips['Sel_B'] == tips['Bias_B']] # In given sample data, there is zero records after filter.

# So, considering original df
tips.groupby(['Mod','Act','Exp','Bias_P','Con_Num','CG_D']).agg({'FCnt':(np.sum,np.size)})
tips.columns = ['Total', 'Count']
print(group_df.reset_index())

   Mod  Act  Exp  Bias_P Con_Num CG_D  Total  Count
0  649    0  GP2       0     SB3  cg1     13      3
1  649    0  GP2       0     SB3  cg2      1      1

您正确地将
sum(FCnt)处理为Total
,对
count(*)使用与Uniq类似的方法,就像单个SQL查询可以做到这一点一样。我们不能在单个panda查询中做同样的事情吗…我已经分别编写了SQL的“where”,分别计算Total…请不要误解我。。我只是想知道这是否有可能SQL的帐户(*)与熊猫的大小()相似……但问题是我无法理解如何编写熊猫查询来派生“Uniq”@HariPriya,您可以直接使用
groupby(…).size()
。@shaikmoeed我试过tips[“Uniq”]=tips.groupby(['Mod','Act','Exp'Bias\P','Con Num','CG\D')).size()这给出了插入列与框架索引不兼容的索引-type error它给了我pandas.hashtable.PyObjectHashTable.get_项(pandas/hashtable.c:12231)中第694行的文件“pandas/hashtable.pyx”KeyError:'FCnt'错误..感谢您的回复…@HariPriya检查名称
FCnt
是否存在于
filtered\u tips.columns
。未生成列FCnt…在filtered\u tips.groupby(['Mod','Act','Exp','Bias\u P','Con Num'CG\u D'])行中({'FCnt':np sum,'Uniq':np size})我拿到钥匙了error@HariPriya过滤提示栏的输出是什么?索引([u'Date',u'Category',u'FCnt',u'TC',u'Mod',u'Con_Num',u'SC',u'Sel_P',u'Bias_P',u'Sel_B',u'Bias_B',u'Act',u'Exp u'CG_D',u'CGM u'P_'val',dtype='object这是输出