[英]How to use groupby, select, count(*) and where commands of SQL together in Pandas
I am new to writing SQL queries in python.我是在 python 中编写 SQL 查询的新手。
I have an SQL query like this.我有一个像这样的 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;
I am trying to convert this query into a python code so that i can perform the operation done by this query using python.我正在尝试将此查询转换为 python 代码,以便我可以使用 python 执行此查询完成的操作。 I came across options using Pandas.
我遇到了使用 Pandas 的选项。
I have the SQL table as.csv format.我有 SQL 表 as.csv 格式。
The code I have written is我写的代码是
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)
But this gives me error for Uniq.但这给了我 Uniq 的错误。 Kindly help me with this code.
请帮助我使用此代码。
Sample Data: (Provided by OP in comments)示例数据:(由 OP 在评论中提供)
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
As you want to take the sum and count of the group, I have used aggregation function in different way ie, .agg({'FCnt':(np.sum, np.size)})
.由于您想获取组的总和和计数,我以不同的方式使用了聚合 function ,即
.agg({'FCnt':(np.sum, np.size)})
。
Code:代码:
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']
Output: Output:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.