How to automate averaging paramecium counts in each csv file in a folder using python

Python scripts

Previously, I took short movie of parameia and counted them in Fiji/ImageJ and now I have a lot of csv files (Summary of Result of …) .  I can use Excel to open the file and calculate the average over frames like this.

But it is a bit tedious to repeat this for 70 files in the folder. So, I leanred how to open a spread sheet file (.csv file) in python (I have installed Anaconda on may PC and using Jupyter to edit and run Python scripts).

Once the script is written, I can go through all the files in the folder just in a moment (much less than one second!).

The script file is the following. Inside the for loop requires an indent. The number of frames in the movie vareis. Hence, iloc[0:,1]) to get the elements in the all rows (from 0th row to the last row) in the “1st” (it is the second column as the first column is the 0th) column. The file name and the average is shown when the script is run for all files in the folder.

import pandas as pd
import numpy as np
from os import listdir
from os.path import isfile, join

mypath=’E:/20181116 NTR 116A/’
onlySummaryFiles = [f for f in listdir(mypath) if isfile(join(mypath, f)) and ‘Summary of Result of’ in f]

for i in range(len(onlySummaryFiles)):
Result = pd.read_csv( filename)
print onlySummaryFiles[i][30:-4],’\t’, np.mean(Result.iloc[0:,1])

print len(onlySummaryFiles)

I’m loving Python.