-1

I'm using Pandas to manipulate a csv file with several rows and columns that looks like the following:

'id'     'cpi'  
 1       0.95   
 1       0.97
 2       0.93
 3       0.98
 4       0.91
 5       0.91
 1       0.86
 2       0.74
 7       0.81

How do I add third column for the total sum for unique id? like:

'id'   'sum'
 1     2.78
 2     1.67
 3     0.98
 4     0.91
 5     0.91
 7     0.81

I'm pretty sure that it should not be super hard just not sure where should I go.

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
Garlok94
  • 1
  • 1
  • 2

2 Answers2

0

groupby.pivot_table(values = "cpi",index=["id"],aggfunc=np.sum)

mhmtktkt
  • 1
  • 1
0

Possible solution is the following:

import pandas as pd

# set data
data = {"id": [1, 1, 2, 3, 4, 5, 1, 2, 7], "cpi": [0.95, 0.97, 0.93, 0.98, 0.91, 0.91, 0.86, 0.74, 0.81]}

# create dataframe
df = pd.DataFrame(data)

df = df.groupby('id').sum()

df.reset_index(inplace=True)

df

Returns

enter image description here

gremur
  • 1,645
  • 2
  • 7
  • 20