Python Forum
Pandas - cumulative sum of two columns
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pandas - cumulative sum of two columns
#7
Okey a different approach for the same problem. I have the same results but instead of points i have a value for either home win (H), draw (D) or away win (A). A win is 3 points for the winning team and draw is 1 point to both teams. I need the cumulative points for both the home and the away team.

This is the dataframe:

df  = pd.DataFrame([
["Gothenburg", "Malmo", 2018,"A"],
["Malmo","Gothenburg",  2018, "D"],
["Malmo", "Gothenburg", 2018, "A"],
["Gothenburg", "Malmo", 2018, "H"],
["Gothenburg", "Malmo" ,2018, "D"],
["Gothenburg", "Malmo", 2018, "A"],
["Gothenburg", "Malmo", 2018, "H"],
["Malmo", "Gothenburg", 2018, "A"],
["Gothenburg", "Malmo", 2018, "H"],
["Malmo", "Gothenburg", 2018, "H"],
[ "Malmo","Gothenburg", 2018, "D"],
[ "Malmo", "Gothenburg",2018, "H"],

])
And this is desired output:
      Hometeam    Awayteam  Year Tecken H_points A_points
0   Gothenburg       Malmo  2018      D      Nan      Nan
1        Malmo  Gothenburg  2018      D        1        1
2        Malmo  Gothenburg  2018      A        2        2
3   Gothenburg       Malmo  2018      D        5        2
4   Gothenburg       Malmo  2018      A        6        3
5   Gothenburg       Malmo  2018      D        6        6
6   Gothenburg       Malmo  2018      A        7        7
7        Malmo  Gothenburg  2018      A       10        7
8   Gothenburg       Malmo  2018      D       10       10
9        Malmo  Gothenburg  2018      A       11       11
10       Malmo  Gothenburg  2018      D       11       14
11       Malmo  Gothenburg  2018      A       12       15
Im losing my hair over this, hope someone can give me a hand :)
Reply


Messages In This Thread
Pandas - cumulative sum of two columns - by tobbs - May-19-2019, 09:29 AM
RE: Pandas - cumulative sum of two columns - by tobbs - May-24-2019, 09:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  iretate over columns in df and calculate euclidean distance with one column in pandas Pit292 0 3,338 May-09-2021, 06:46 PM
Last Post: Pit292
  pandas.to_datetime: Combine data from 2 columns ju21878436312 1 2,480 Feb-20-2021, 08:25 PM
Last Post: perfringo
  Remove extra count columns created by pandas groupby spyf8 1 2,752 Feb-10-2021, 09:19 AM
Last Post: Naheed
  Pandas: summing columns conditional on the column labels ddd2332 0 2,157 Sep-10-2020, 05:58 PM
Last Post: ddd2332
  Difference of two columns in Pandas dataframe zinho 2 3,398 Jun-17-2020, 03:36 PM
Last Post: zinho
  Pandas dataframe columns collapsed in Spyder when printing UniKlixX 2 4,804 Nov-04-2019, 07:00 AM
Last Post: UniKlixX
  [pandas] How to re-arrange DataFrame columns SriMekala 8 4,977 Jun-22-2019, 12:55 AM
Last Post: scidam
  comparing two columns two different files in pandas nuncio 0 2,425 Jun-06-2018, 01:04 PM
Last Post: nuncio
  subtruction of columns in pandas garikhgh0 3 2,688 May-14-2018, 10:31 AM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020