I have the following data frame:

    Race Course                 Horse  Year  Month  Day  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

I am trying to create another data frame which includes only the sum of all races (rows) and the sum of all races won. It would ideally look like the following:

total races     18
total won       11

However, all I have been able to do is group by counts, counting total won and total lost. This is what I have attempted:

df = df.groupby(['Won/Lost']).size().add_prefix('total')

And this is what it returns:

Won/Lost
total lost     7
total won     11
dtype: int64

I am at a dead end and cannot figure out a simple solution.

1 Answers

2
Supratim Haldar On

Assuming content of races.csv is:

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

Steps to get the new dataframe:

>>> races_df = pd.read_csv('races.csv')
>>> races_df
    Race Course                Horse  Year  Month  Day  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
>>>
>>> total_races = len(races_df)
>>>
>>> total_win = races_df[races_df['Won/Lost'] == 'won']['Won/Lost'].count()
>>>
>>> new_df = pd.DataFrame({'total_races': total_races, 'total_win': total_win}, index=pd.RangeIndex(1))
>>>
>>> new_df
   total_races  total_win
0           18         11