Home > Mobile >  How i can calulate the sum from a csv file in python?
How i can calulate the sum from a csv file in python?

Time:11-27

Hy guys i have one problem with the python. I just start to learn and my teacher has give me the task with the python. So the problre is how i can calulate the sum from a column in csv file in pyhon.

Here is my tries :

import pandas as pd

df = pd.read_csv('1.csv')
dfsum=sum(df)

print(dfsum)

My termial is compley for i a do sum with str on integers.

Also the csv file is like this :

-12
-14
-76
-89
-98
-45
-26

CodePudding user response:


So I dont know if your csv file contains only negative number or it's juste the way you present it, but what i can say is that you can try this it gonna work eitherway :
df = pd.read_csv("1.csv", names=["my_column"])
dfsum = df["my_column"].sum()

assuming you have all negative numbers the result is : -360

  • Related