[英]Filter by grouping two columns and sum third column data python
I have an assignment to do for college, where I need to write a python code that lists years, total won, and total lost of bets that took place. 我有一个为大学做的任务,我需要编写一个python代码,列出年份,总赢额和所发生的投注总损失。
My data is in a .csv file with headings 我的数据位于带有标题的.csv文件中
Race_Course
Horse Name
Year
Month
Day
Amount_won_lost
Win/Los
I need to print out the following: 我需要打印出以下内容:
Year Total Won Total Lost
2016 €xxxxx €xxxxx
2017 €xxxxx €xxxxx
I have tried the following: 我尝试过以下方法:
total = df.groupby(['Year','Win/Loss']).Amount_won_lost.sum().reset_index(level=1)
print(total)
which prints: 打印:
Year Win/Loss Amount_won_lost
2016.0 lost 115.00
2016.0 won 584.81
2017.0 lost 5.00
2017.0 won 69.31
How do I rearrange the total won and total lost for each year? 如何重新安排每年的总赢额和总损失?
添加到最后并且它工作:
print(total_won_2016.pivot(index=None, columns='Win/Loss', values='Amount_won_lost'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.