Feb-04-2019, 09:59 PM
Here is my code and everything works fine. There are over 12000 lines in the CSV file.
My question is, does pandas read in the entire file before anything below the line runs?
Is there any way to speed this script up? I only really use the last 28 days of the file.
My question is, does pandas read in the entire file before anything below the line runs?
Is there any way to speed this script up? I only really use the last 28 days of the file.
import pandas as pd import datetime import wx # --------------------------------- read from csv --------------------------- df = pd.read_csv('summary.csv', delimiter='\t', skiprows=3, header=None, names=[ 'id', 'rdate', 'rtype', 'hist', 'scan'], usecols=[0, 1, 2, 3, 4]) # ---------- Fill in 0 --------------------------- df['hist'] = df['hist'].fillna(0) df['scan'] = df['scan'].fillna(0) # -------------- Change dtypes ------------------- df['rdate'] = df['rdate'].astype('datetime64') df['hist'] = df['hist'].astype('int64') df['scan'] = df['scan'].astype('int64') # -------------------------------- move value from scan to hist --------------------------------- df.loc[df['hist'] == 0, 'hist'] = df['scan'] # --------------------------- Select from DataFrame -------------- sd = (df.loc[df['rdate'] > datetime.datetime.now() - pd.to_timedelta("28day")]) sr = (sd.loc[sd['rtype'] == 0]) # ---------------------------- Do calculations ------------------------------- average = round(sr['hist'].mean()) a1c = round((46.7 + sr['hist'].mean() + sr['scan'].mean()) / 28.7, 1) # -----------------Display in wx.Frame ---------------------------------- class Example(wx.Frame): def __init__(self, *args, **kw): super(Example, self).__init__(*args, **kw) self.SetBackgroundColour('#85C1E9') self.InitUI() def InitUI(self): pnl = wx.Panel(self) font = wx.Font(16, wx.FONTFAMILY_SCRIPT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD) font2 = wx.Font(12, wx.FONTFAMILY_SCRIPT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL) font3 = wx.Font(11, wx.FONTFAMILY_SCRIPT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL) heading = wx.StaticText(pnl, label='Average Glucose and A1c', pos=(50, 15), size=(200, -1)) heading.SetFont(font) st3 = wx.StaticText(pnl, label=sr.rdate.max().strftime('From: %b %d %y') + ' - ' + str(sr.rdate.min().strftime('To: %b %d %y')), pos=(70, 45)) wx.StaticLine(pnl, pos=(6, 65), size=(328, 1)) st1 = wx.StaticText(pnl, label='Average: ' + str("%.0f" % average), pos=(125, 80)) st2 = wx.StaticText(pnl, label='A1c: ' + str(a1c), pos=(125, 100)) st1.SetFont(font2) st2.SetFont(font2) st3.SetFont(font3) btn = wx.Button(pnl, label='Close', pos=(140, 150)) btn.Bind(wx.EVT_BUTTON, self.OnClose) self.SetSize((360, 230)) self.SetTitle('A1c') self.Centre() def OnClose(self, e): self.Close(True) def main(): app = wx.App() ex = Example(None) ex.Show() app.MainLoop() if __name__ == '__main__': main()
Gary