python - Adding values of a pandas column only at specific indices -


i working on pandas data frame, below:

   id        vals 0   1          11 1   1         5.5 2   1          -2 3   1           8 4   2           3 5   2           4 6   2          19 7   2          20 

above small part of df, vals grouped id , , there equal number of vals per id. in above case it's 4 , 4 values id = 1 , id =2.

what trying achieve add value @ index 0 value @ index 4, value @ index 1 value @ index 5 , on. following expected df/ series, df2:

    total 0      14 1     9.5 2      17 3      28 

real df has hundreds of id , not 2 above. groupby() can used dont how specific indices each group.

please let me know if unclear.

groupby on modulo of df.index values , take sum of vals

in [805]: df.groupby(df.index % 4).vals.sum() out[805]: 0    14.0 1     9.5 2    17.0 3    28.0 name: vals, dtype: float64 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -