'how do I import the data of a csv file per category and sum it?

I am new to coding and am having some problems making my code work. What I have is three columns, which have the same category. one column is a string value, ie children, and the other two discuss grams and micrograms. I want to get a sum of the combined total micrograms and grams in the specified category (in this example that is children) but instead, my code is only returning either a list of the values, or error codes. What do I need to fix?

Category Grams Micrograms
Children 2 99,99
Children 1 4
import numpy as np  
import matplotlib.pyplot as plt   
import pandas as pd
filename = '/Users/Desktop/20220503_python_input_template.csv'

df = pd.read_csv(filename,delimiter=';',decimal=',',na_values=-9999.99)

df = df[df['mass per item (g)'].notna()]


cat = df["Category"]
subcat = df["Sub-category"]
m_p = df["mass per item (g)"]
m_mp = df["mass micro per item (g)"]
m_fe = df["Frequency as factor"]

print("Do You have any children?\n1) No\n2) Yes- 1\n3) Yes-2\n4) Yes-3")#

val1 = int(input("Enter value: "))

info = df.loc[cat.str.contains("Children", case=False)]

idx=df.index[cat=='Children'].tolist() 
total+= m_p.iloc[idx] + mm_p.iloc[idx] 

print(total)

else:
print("Invalid")


Solution 1:[1]

If you only need sum of grams and micrograms per category:

import pandas as pd
filename = '/Users/Desktop/20220503_python_input_template.csv'
df = pd.read_csv(filename,delimiter=';',decimal=',',na_values=-9999.99)
df.groupby(by='Category').sum()

Saying “thanks” is appreciated, but it doesn’t answer the question. Instead, vote up the answers that helped you the most! If these answers were helpful to you, please consider saying thank you in a more constructive way – by contributing your own answers to questions your peers have asked here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jpg997