【问题标题】:How to use groupby, select, count(*) and where commands of SQL together in Pandas如何在 Pandas 中同时使用 SQL 的 groupby、select、count(*) 和 where 命令
【发布时间】:2020-07-02 08:20:40
【问题描述】:

我是使用 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 执行此查询完成的操作。我遇到了使用 Pandas 的选项。

我有 .csv 格式的 SQL 表。

我写的代码是

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 在 cmets 中提供)

Date,Category,FCnt,TC,Mod,Con_Num,SC,Sel_P,Bias_P,Sel_B,Bias_B,Act,Exp,CG_D,CGM,P_val
20200622,T1,5,RE,649,SB3,01,0,0,0,1,0,GP2,cg1,0,Pattern1
20200622,T1,1,RE,649,SB3,10,1,0,0,1,0,GP2,cg2,0,pattern2
20200622,T1,4,RE,649,SB3,11,0,0,0,1,0,GP2,cg1,0,pattern1
20200622,T1,4,RE,649,SB3,11,1,0,0,1,0,GP2,cg1,0,pattern1

【问题讨论】:

  • 你正确处理了sum(FCnt) as Total,对count(*) as Uniq使用类似的方法
  • 就像单个 SQL 查询如何做到这一点......我们不能在单个 panda 查询中做同样的事情......我已经单独编写了 SQL 的“where”,分别计算 Total.. ..请不要误会我的意思..我只是想知道这是否可能
  • SQL 的 count(*) 类似于 pandas 的 size() ...但问题是我无法理解如何编写 panda 查询以导出“Uniq”
  • @HariPriya 你可以直接使用groupby(...).size()
  • @shaikmoeed 我尝试了tips["Uniq"]=tips.groupby(['Mod','Act','Exp','Bias_P','Con_Num','CG_D'])。 size() 这给出了带有框架索引的插入列的不兼容索引 - 类型错误

标签: python sql pandas


【解决方案1】:

由于您想获取组的总和和计数,我以不同的方式使用聚合函数,即.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

【讨论】:

  • 它给了我文件“pandas/hashtable.pyx”,第 694 行,在 pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12231) KeyError: 'FCnt' error..Thanks for回复...
  • @HariPriya 检查名称FCnt 是否存在于filtered_tips.columns 中。
  • FCnt 列未生成...在行 filters_tips.groupby(['Mod','Act','Exp','Bias_P','Con_Num','CG_D']) .agg({'FCnt':np.sum,'Uniq':np.size}) 我得到密钥错误
  • @HariPriya filtered_tips.columns 的输出是什么?
  • 索引([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') 这是输出
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-01
  • 2022-10-12
  • 2013-04-10
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多