OFFENSE_ID RULE_ID STATUS
100 1 open
101 2 closed
102 2 open
103 1 open
104 2 closed
105 2 open
这就是我想要的-
RULE_ID OPEN CLOSED
1 2 0
2 2 2
在此先感谢您的帮助!
答案 0 :(得分:1)
以下代码生成所需的输出。如果您可以提供必要的脚本来模拟场景,那么其他人就可以轻松进行工作。
df=pd.DataFrame([[100,1,"open"],[101,2,"closed"],[102,2,"open"],[103,1,"open"],[104,2,"closed"],[105,2,"open"]],columns=["OFFENSE_ID","RULE_ID","STATUS"])
df["OPEN"]=df[df["STATUS"]=="open"]["STATUS"]
df["CLOSED"]=df[df["STATUS"]=="closed"]["STATUS"]
dfGroup=df.groupby(by=["RULE_ID"],as_index=False).agg({"OPEN":"count","CLOSED":"count"})
输出:
答案 1 :(得分:0)
尝试
dfinal = df1.merge(df2, how='inner', left_on='RULE_ID', right_on='RULE_ID')