-6

I have:

df = pd.DataFrame({
  "ID": [55218,55218,55218,55222],
  "Cluster": [0,0,1,1],  
  "Rating":[-1,2,0,2]})

I want to sum the Rating of e.g. Cluster "0" for every "ID"

My expected output is:

df_new = pd.DataFrame({
  "ID": [55218,55218,55218,55222], 
  "Cluster": [0,1,1], 
  "Sum": [1,0,2] })
IoaTzimas
  • 10,263
  • 2
  • 10
  • 29
Mars
  • 41
  • 6
  • Please retake the [tour], read [what's on-topic here](/help/on-topic), [ask], and the [question checklist](//meta.stackoverflow.com/q/260648/843953), and provide a [mre]. "Implement this feature for me" is off-topic for this site. You have to _make an honest attempt_, and then ask a _specific question_ about your algorithm or technique. – Pranav Hosangadi Nov 03 '20 at 16:11

1 Answers1

-1

Use groupby

df.groupby("ID")["Cluster"].sum().reset_index()
Wasif
  • 13,656
  • 3
  • 11
  • 30
  • 2
    Questions as basic as this one have usually been asked and answered. Please look for duplicates before posting simple, one-line answers. – Pranav Hosangadi Nov 03 '20 at 16:16