简体   繁体   English

通过对两列进行分组并对第三列数据python求和来进行筛选

[英]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.

相关问题 Python:将两列组合在一起,找到第三列的总和 - Python: Group two columns together and find sum of third column Python:如果两列具有相同的值,则为第三列的和值 - Python: sum values of the third column if two columns have the same value 用值分组两列以获得第三列 - Grouping two columns with values to get a third column 按两列分组并根据两列条件添加第三列 - Grouping by two columns and add a third column based on a condition of two columns 大熊猫:按两列分组,然后按第三列的值排序 - pandas: Grouping by two columns and then sorting it by the values of a third column 比较两列以在 python 中创建第三列 - Compare two columns to create third column in python 对numpy数组的两列求和,并将其添加为数组的第三列 - Sum two columns of a numpy array and add it as a third column of the array 有没有办法在使用 pandas 按第三列中的值分组时将两列中的值相乘? - Is there a way to multiply the values in two columns while grouping by values in third column using pandas? 在两列上创建条件并创建第三列失败的Pandas Python - Condition on Two columns and create third column failed Pandas Python Python - 查找按第三列分组的列中两个事件之间的平均差异 - Python - Find mean difference between two events in a column grouping by a third column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM