Sum function in a Pandas data frame

I am trying to fix a β€œrunning” cumulative amount, given a series of periods.

See an example:

enter image description here

df = df[1:4].cumsum() # this doesn't return the desired result 
+5
source share
2 answers

You are looking for the axis parameter. Many Pandas functions accept this argument to apply a column or row operation. Use axis=0 for applying in different ways and axis=1 for applying in columns. This operation actually intersects the columns, so you want axis=1 .

df.cumsum(axis=1) by itself works on your example to create an output table.

 In [3]: df.cumsum(axis=1) Out[3]: 1 2 3 4 10 16 30 41 61 51 13 29 40 50 13 11 30 45 61 321 12 27 37 52 

I suspect you are interested in limiting a certain range of columns. For this you can use .loc with column labels (rows in the mine).

 In [4]: df.loc[:, '2':'3'].cumsum(axis=1) Out[4]: 2 3 10 14 25 51 16 27 13 19 34 321 15 25 

.loc is method-based and contains estimates. If you want to know more about indexing in Pandas, check out the docs .

+1
source

You want axis=1 sum the rows.

 df.cumsum(axis=1) 

Side note - executing [1:4] cuts the default lines (ie numpy or list-like semantics). If you want to select columns by label, use df.loc[:, 1:4]

+2
source

Source: https://habr.com/ru/post/1234401/


All Articles