-1
Adi  70   math's     Bangalore    2022
vira 80   math's     Bangalore    2022
Adi  30   English    Bangalore    2022

Result

Adi 100 maths bangalore 2022
AKX
  • 152,115
  • 15
  • 115
  • 172
  • Why would you expect to get that result? – AKX Sep 11 '22 at 11:51
  • `df.groupby(['Name','Year','City']).sum()` if 'Name', 'Year', and 'City' are the names of the columns 0, 4, and 3. Please note, that Adi has the numbers 70 and 30 for two distinct classes, while in you final table you arbitrarily assign 'maths' to the sum. – Alex Sep 11 '22 at 11:52
  • Does this answer your question? [How do I Pandas group-by to get sum?](https://stackoverflow.com/questions/39922986/how-do-i-pandas-group-by-to-get-sum) – sushanth Sep 11 '22 at 11:56

1 Answers1

0

Assuming maths in the result is a typo and you want to sum up all marks for a given name/city/year combination,

import io

import pandas as pd

df = pd.read_csv(io.StringIO("""
Adi;70;math's;Bangalore;2022
vira;80;math's;Bangalore;2022
Adi;30;English;Bangalore;2022
""".strip()), sep=";", names=["name", "marks", "subject", "city", "year"])

gdf = df.groupby(["name", "city", "year"]).agg({"marks": "sum"}).reset_index()
print(gdf)
   name       city  year  marks
0   Adi  Bangalore  2022    100
1  vira  Bangalore  2022     80
AKX
  • 152,115
  • 15
  • 115
  • 172