specmer
specmer

Reputation: 409

Sum Each Column of CSV file

I have a large CSV file with 32 column headers. I'd like to sum up each column and the result be 32 individual summations of each column header. I have access to both python and powershell. Any help would be appreciated.

The furthest I got was this site: pandas groupby with sum() on large csv file?

Upvotes: 2

Views: 4559

Answers (5)

Sam Critchley
Sam Critchley

Reputation: 3798

In powershell (or Linux/Mac OS etc) you should be able to install the excellent CSVFIX command-line package (which works very fast on large CSV files and also has a Windows installer).

You can use the CSVFIX summary command to generate a sum of each column:

csvfix summary -sum 1:32 filename.csv

This will give you a single-line summary of the sum of each column:

"43", "21", "425", "1092", [...]

If the file has a header row don't forget to also add the -ifn flag to ignore the first row.

Upvotes: 1

import csv
with open('yourBigFile.csv', 'rb') as f:
    spreadsheet=csv.reader(f) #you may need some options 
                              #depending on the format of the file
    header=None
    for row in spreadsheet:
        if header is None:
            header=row
            mySums=[0]*len(row) #  initialize to zero
            continue
        else:
            # this will only work if every cell has a number
            #   this will be faster, so use it if it is possible
            #   in your application
            #mySums=[mySums[x]+float(row[x]) for x in range(len(mySums))]

            # more generally
            for i,x in enumerate(row):
                try:
                    converted=float(x)
                except ValueError:   #you may actually want an error
                                     #raised.  YMMV depending on your data
                    converted=0
                mySums[i]+=converted

As I am not sure how you want the output to be formatted, I will leave that to you.

Upvotes: 1

Vincent Claes
Vincent Claes

Reputation: 4768

import pandas as pd
pd.read_csv(r'my_path_to_file/my_file.csv', sep=';').sum().values

Pandas is definitly the way to go . these two lines of code will print out the sum of the columns. if you are on windows use a '\' for specifying your path. I assume your csv file uses a semicolon as a seperator (if its a comma use sep=',' if its a tab use sep='\t')

If you want to write the result to a file use:

import pandas as pd
df = pd.read_csv(r'my_path_to_file/my_file.csv', sep=';').sum()
df.to_csv(r'my_path_to_file/my_file_sum.csv')

Upvotes: 1

Dilettant
Dilettant

Reputation: 3335

A simple way using only builtins on this sample data file:

#! /usr/bin/env python
from __future__ import print_function

sep = ';'
with open('32_numeric_columns.csv', 'rt') as f:
    columns = f.readline().strip().split(sep)
    rows = [0] * len(columns)
    for line in f.readlines():
        data = line.strip().split(sep)
        for i, cell in enumerate(data, start=0):
            rows[i] += float(cell)


print(columns)
print(rows)

on this data file:

a0;a1;a2;a3;a4;a5;a6;a7;a8;a9;b0;b1;b2;b3;b4;b5;b6;b7;b8;b9;c0;c1;c2;c3;c4;c5;c6;c7;c8;c9;d0;d1
1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1
1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1
1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1
1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1
1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1
1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1
1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1
1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1
1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1
1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1

yields:

['a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'b0', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9', 'c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'd0', 'd1']
[10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0]

Working on a huge file with 1280000000 bytes of data took approx. 5 minutes on my machine to produce:

$> time ./so_csv_adder.py
['a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'b0', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9', 'c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'd0', 'd1']
[20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0, 20000000.0]

real    4m47.374s
user    4m43.748s
sys 0m2.545s

Upvotes: 1

Batman
Batman

Reputation: 8927

You can use read_csv in pandas to read the file, and then just use sum() on the dataframe.

import pandas as pd

filename = r'folder/file.txt'
df = pd.read_csv(filename)
total = df.sum()

Upvotes: 0

Related Questions