Python Forum
Information how pandas works
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Information how pandas works
#1
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.

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
Reply


Messages In This Thread
Information how pandas works - by gehrenfeld - Feb-04-2019, 09:59 PM
RE: Information how pandas works - by stullis - Feb-05-2019, 02:38 AM
RE: Information how pandas works - by scidam - Feb-05-2019, 11:30 AM
RE: Information how pandas works - by gehrenfeld - Feb-05-2019, 12:29 PM
RE: Information how pandas works - by scidam - Feb-06-2019, 12:22 AM
RE: Information how pandas works - by gehrenfeld - Feb-06-2019, 01:00 PM
RE: Information how pandas works - by scidam - Feb-07-2019, 12:41 AM
RE: Information how pandas works - by gehrenfeld - Feb-07-2019, 12:51 PM
RE: Information how pandas works - by scidam - Feb-07-2019, 11:44 PM
RE: Information how pandas works - by gehrenfeld - Feb-08-2019, 01:09 PM
RE: Information how pandas works - by scidam - Feb-09-2019, 11:25 AM
RE: Information how pandas works - by gehrenfeld - Feb-09-2019, 12:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I convert my data so it works with Pandas? Oliver 0 2,421 Dec-11-2017, 04:09 PM
Last Post: Oliver

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020