给定标志更改项目的值

时间:2019-04-08 21:46:29

标签: python pandas dataframe

我有一个数据框,其中包含对马匹的投注,每一行都是新的投注。每个投注具有多个属性,包括位置,马的名字,赢/输等。 问题在于,那些下注的奖金以正整数给出,并且提供了flag属性以说明是赢还是输。

这是提供的数据框:

    Race Course                 Horse  Year  Month  Date  Amount Won/Lost
0       Aintree               Red Rum  2017      5    12   11.58      won
1   Punchestown               Camelot  2016     12    22  122.52      won
2       Sandown        Beef of Salmon  2016     11    17   20.00     lost
3           Ayr              Corbiere  2016     11     3   25.00     lost
4    Fairyhouse               Red Rum  2016     12     2   65.75      won
5           Ayr               Camelot  2017      3    11   12.05      won
6       Aintree         Hurricane Fly  2017      5    12   11.58      won
7   Punchestown        Beef or Salmon  2016     12    22  112.52      won
8       Sandown              Aldaniti  2016     11    17   10.00     lost
9           Ayr   Henry the Navigator  2016     11     1   15.00     lost
10   Fairyhouse               Jumanji  2016     10     2   65.75      won
11          Ayr           Came Second  2017      3    11   12.05      won
12      Aintree                Murder  2017      5    12    5.00    lost
13  Punchestown           King Arthur  2016      6    22   52.52      won
14      Sandown         Filet of Fish  2016     11    17   20.00     lost
15          Ayr                Denial  2016     11     3   25.00     lost
16   Fairyhouse          Don't Gamble  2016     12    12  165.75      won
17          Ayr               Ireland  2017      1    11   22.05      won

我需要创建以下格式的数据框:

Year        Total Won   Total Lost
2016        €123.45     €678.90
2017        €543.21     €987.60

我一直在尝试遍历各列,并尝试使用where函数,但似乎无法使任何事情起作用。

1 个答案:

答案 0 :(得分:4)

使用groupbysum,然后堆叠结果:

df.groupby(['Year', 'Won/Lost'])['Amount'].sum().unstack(-1).add_prefix('total_')

Won/Lost  total_lost  total_won
Year                           
2016           115.0     584.81
2017             5.0      69.31