Python - Calculate average for every column in a csv file
Here's a clean up of your function, but it probably doesn't do what you want it to do.
Currently, it is getting the average of all values in all columns:
def average_column (csv):
f = open(csv,"r")
average = 0
Sum = 0
row_count = 0
for row in f:
for column in row.split(','):
n=float(column)
Sum += n
row_count += 1
average = Sum / len(column)
f.close()
return 'The average is:', average
I would use the csv
module (which makes csv parsing easier), with a Counter
object to manage the column totals and a context manager to open the file (no need for a close()
):
import csv
from collections import Counter
def average_column (csv_filepath):
column_totals = Counter()
with open(csv_filepath,"rb") as f:
reader = csv.reader(f)
row_count = 0.0
for row in reader:
for column_idx, column_value in enumerate(row):
try:
n = float(column_value)
column_totals[column_idx] += n
except ValueError:
print "Error -- ({}) Column({}) could not be converted to float!".format(column_value, column_idx)
row_count += 1.0
# row_count is now 1 too many so decrement it back down
row_count -= 1.0
# make sure column index keys are in order
column_indexes = column_totals.keys()
column_indexes.sort()
# calculate per column averages using a list comprehension
averages = [column_totals[idx]/row_count for idx in column_indexes]
return averages
calculating Average of each cell across multiple csv
import pandas as pd
df=pd.read_csv("a1.csv")
for i in range(2,21):
filename="a"+str(i)+".csv"
df+=pd.read_csv(filename)
df=df/20
This should do the trick..
Related Topics
Running an Excel Macro Via Python
How to Create String With Line Breaks in Python
Selenium Python Send_Key Error: List Object Has No Attribute
How to Handle Multiple Keys for a Dictionary in Python
How to Write a Lambda Function That Is Conditional on Two Variables (Columns) in Python
Python How to Remove Escape Characters from a String
How to Match a Newline Character in a Raw String
How to Install Tesseract for Python on Anaconda
Plotting Data from Multiple Pandas Data Frames in One Plot
How to Save Xlsm File With Macro, Using Openpyxl
Python Not Working in the Command Line of Git Bash
How to Create an Automatically Updating Gui Using Tkinter
How to Split Folder of Images into Test/Training/Validation Sets With Stratified Sampling
Iterating Over Every Two Elements in a List
Find Matching Rows in 2 Dimensional Numpy Array