0

How do you sum up only the steel with Pandas?

Date                   Material         Quantity
10-12-2020             steel            2
12-12-2020             steel            5
13-12-2020             steel            6
14-12-2020             glass            1
15-12-2020             glass            2
16-12-2020             plastic          10

So, far I have done this, which displays only the 'steel' material, but I have difficulties to sum them:

import pandas as pd
df= pd.read_csv('materials.cvs')
total_materials = df.groupby('material')
materials = total_materials.get_group('steel')
print(materials)
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • Looks like someone has already answered this question. https://stackoverflow.com/questions/48133983/sum-a-range-of-cells-in-a-single-column-in-pandas-dataframe – Jay Lynch Dec 30 '20 at 20:30

1 Answers1

0

Try this

steel = df.loc[df['Material'] == 'steel']
total = steel['Quantity'].sum()
print("Result: "+str(total))
Ignacio Alorre
  • 7,307
  • 8
  • 57
  • 94