我希望得到每月完成的交易占总交易的百分比。以前,我的数据只有一个月,通过以下方式解决:
total_trades = df['state'].count()
RFQ_Hit_Rate = done_trades / total_trades
RFQ_Hit_Rate = round(RFQ_Hit_Rate, 6)
现在有12个月的数据,所以我需要更新代码。新数据
dfHit_Rate_All = df[['Year_Month','state']].copy()
dfHit_Rate_All = dfHit_Rate_All.groupby(['Year_Month','state']).size().reset_index(name='count')
Year_Month state Counts
2017-11 Customer Reject 1
2017-11 Customer Timeout 2
2017-11 Dealer Reject 3
2017-12 Dealer Timeout 4
2017-12 Done 5
2017-12 Done 6
2018-01 Tied Covered 7
2018-01 Tied Done 8
2018-01 Tied Traded Away 9
2018-02 Traded Away 10
2018-02 Done 11
2018-02 Customer Reject 12
对于每个月,找出总交易,总成交交易,并计算比率。请注意,任何带有'Done'的字符串都是已完成的交易,即[df['state']。结构包含(“完成”):
Year_Month Total_state_count Total_state_count_Done Done_To_Total_Ratio
2017-11 6 0 0%
2017-12 15 11 73%
2018-01 24 8 33%
2018-02 33 11 33%
我认为需要通过^{} 聚合元组-使用聚合函数的新列名:
对于新列,除以
100
:如果需要将最后一列转换为整数并添加百分比:
相关问题 更多 >
编程相关推荐