Home > Mobile >  Python: How to calculate sum of floats on csv
Python: How to calculate sum of floats on csv

Time:11-30

I'm starting to learn Python. How do I calculate the sum (float) of a certain column on csv file?

This is what I've done so far, but my code is not giving me the total of 'amount' (column):

giftFile = open('input.v0.small.csv')
giftReader = csv.reader(giftFile)
giftData = list(giftReader)

for row in giftReader:
    if len(row)>0:
        giftData  = row['amount']

print('row 0:'   str(giftData[0]))
print("row 1's dollar value: "   str(giftData[1]))

CodePudding user response:

You can do this easily with sum. csv.reader returns an iterable giving a list for each row, so we just need to pick out the right column element, convert to a float, then add them all up. This example uses Decimal to give 'expected' results when summing floats, but you can use float instead if you prefer:

import csv
from decimal import Decimal

with open('input.v0.small.csv') as giftFile:
    print(sum(Decimal(x[1]) for x in csv.reader(giftFile)))

Simply change x[1] for a different element to pick out a different column.

CodePudding user response:

When opening files in Python, files should be closed. To avoid forgetting to close, use a context manager. After reading in your data as a list of lists, you could use a list comprehension to extract data from a specified column. Then, use the sum built-in function to perform the final summation.

from csv import reader

# context manager for reading-in CSV file 
with open('input.v0.small.csv', 'r') as read_obj:
    csv_reader = reader(read_obj)
    data = list(csv_reader)

col_num = 0 # assuming you want the first column; modify otherwise

# list comprehension to extract specified column
ls = [row[col_num] for row in data] 

# built-in sum function
column_sum = sum(ls)

# print result
print('Column sum is {column_sum}')

CodePudding user response:

You could try:

import csv
from math import fsum

with open('input.v0.small.csv', 'r') as file:
    result = fsum(float(d.get('amount', 0)) for d in csv.DictReader(file))
  • Related