I am in a lower-level coding class (Python) and have a major project due in three days. One of our grading criteria is program speed. My program runs in about 30 seconds, ideally it would execute in 15 or less. Here is my code:

```
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
import time

start_time = time.time()#for printing execution time

#function for appending any number of files to a dataframe

def read_data_files(pre, start, end): #reading in the data
    data = pd.DataFrame()#dataframe with all the data from files
    x = start

    while x <= end:
        filename = pre + str(x) + ".csv" #string manipulation
        dpath = pd.read_csv("C:\\Users\\jacks\\Downloads\\BMEN 207 Project 1 
        Data\\" + filename )

        for y in dpath:
            dpath = dpath.rename(columns = {y: y})

        data = data.append(dpath)
        x += 1

    return data

data = read_data_files("Data_", 5, 163) #start, end, prefix...

#converting to human time and adding to new column in dataframe
human_time = []

for i in data[' Time']:
    i = int(i)
    j = datetime.utcfromtimestamp(i).strftime('%Y-%m-%d %H:%M:%S')
    human_time.append(j)

human_timen = np.array(human_time) #had issues here for some reason, so i 
created another array to replace the time column in the dataframe
data[' Time'] = human_timen

hours = [] #for use as x-axis in plot
stdlist = [] #for use as y-axis in plot
histlist = [] #for storing magnitudes of most active hour

def magfind(row): #separate function to calculate the magnitude of each row in 
each dataframe
    return (row[' Acc X'] ** 2 + row[' Acc Y'] ** 2 + row[' Acc Z'] ** 2) ** .5

def filterfunction(intro1, intro2, first, last): #two different intros to deal 
with the issue of '00:' versus '10:' timestamps
    k = first
    meanmax = 0 
    active = 0

    while k <= last:
        if 0 <= k < 7: #data from hours 0 to 6, none after
            hr = intro1 + str(k) + ':'
            tfilter = data[' Time'].str.contains(hr)
            acc = data.loc[tfilter, [' Acc X', ' Acc Y', ' Acc Z']]
            acc['magnitude'] = acc.apply(magfind, axis = 1) #creates magnitude 
column using prior function, column has magnitudes for every row of every file
            p = acc.loc[:, 'magnitude'].std()#finds std dev for the column and 
appends to a list for graphing
            m = acc.loc[:, 'magnitude'].mean()
            stdlist.append(p)            

        elif k == 12 or 20 < k <= last: #data at 12 and beyond hour 20
            hr = intro2 + str(k) + ":"
            tfilter = data[' Time'].str.contains(hr)
            acc = data.loc[tfilter, [' Acc X', ' Acc Y', ' Acc Z']]
            acc['magnitude'] = acc.apply(magfind, axis = 1)
            p = acc.loc[:, 'magnitude'].std()
            m = acc.loc[:, 'magnitude'].mean()
            stdlist.append(p)

        else: #in the case that we are dealing with an hour that has no data
            p = 0
            m = 0
            stdlist.append(p) #need this so that the hours with no data still 
get graphed 
        if m > meanmax: # for determining which hour was the most active, and 
appending those magnitudes to a list for histogramming
            meanmax = m
            active = k #most active hour
            for i in acc['magnitude']:
                histlist.append(i) #adding all the magnitudes for histogramming

        hours.append(k)
        k += 1
    print("mean magnitude", meanmax)
    print("most active hour", active)

    return hours, stdlist, histlist

filterfunction(' 0', ' ', 0, 23)

```

The slow speed stems from the "filterfunction" function. What this program does is read data from over 100 files, and this function specifically sorts the data into a dataframe and analyzes by time (each individual hour) in order to calculate the data in all rows for that hour. I believe that it might be able to be sped up by changing up the way that the data is filtered to search by hour, but am not sure. The reason I have statements to dis-include certain k-values is that there are hours with no data to manipulate, which would mess up the list of standard deviation calculations as well as the plot that this data will father. Any tips or ideas for speeding this up would be greatly appreciated!