Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.052 X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; 'toss': 0.09; 'cc:addr :python-list': 0.11; 'caching': 0.16; 'col': 0.16; 'columns': 0.16; 'expert,': 0.16; 'third,': 0.16; ':-)': 0.16; 'ignore': 0.16; 'cc:addr:python.org': 0.22; 'load': 0.23; 'mind.': 0.24; 'skip': 0.24; 'cc:2**0': 0.24; '>': 0.26; 'first,': 0.26; 'header:In-Reply-To:1': 0.27; 'skip:p 30': 0.29; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'second,': 0.31; 'them?': 0.31; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'little': 0.38; 'even': 0.60; 'deleting': 0.60; 'save': 0.62; 'more': 0.64; 'android.': 0.84; 'column.': 0.84; 'measure.': 0.84; 'pickled': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=n8gsvg7zxLXf7MPz4GbM4jVj2jzKu7xw+dhsrd7mrpU=; b=LNXDESUY1XqAgQxlih/rhtn/3ADWULaO4gjZxFAEvlnQXJY+1bqmjDUO4dWqEPPg+P 7hyMKHrIw+MDVTHBj0yb2QWasJCcBKcZPE9bsDJdrc3l3jGCX1xLJm7fS3FMGxxYQJS2 t5+nxNcPJTvDxFQVjRkV0fuYR3OzYZrqBPCHpeOt/wEa9Wc/aa5lRLxVdc6eb/P4rozO FmPvfrRUAe3OYZXLjQf/Mdndw8YSJbwRfPgIR2eVUFg3xaD+B/b5oyXJr1cmIfG2zlyM kEhF6pprVExjrZFqbYt0ND01rpT+NXc7HJHjTgCpxqTu92owu0EwYmYlH6ol4Ph7oCHV NNEg== MIME-Version: 1.0 X-Received: by 10.42.82.6 with SMTP id b6mr9691792icl.51.1406764666916; Wed, 30 Jul 2014 16:57:46 -0700 (PDT) In-Reply-To: References: Date: Wed, 30 Jul 2014 18:57:46 -0500 Subject: Re: speed up pandas calculation From: Skip Montanaro To: Vincent Davis Content-Type: multipart/alternative; boundary=485b397dd70141177804ff71ecad Cc: Python X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1406764670 news.xs4all.nl 2850 [2001:888:2000:d::a6]:38589 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75391 --485b397dd70141177804ff71ecad Content-Type: text/plain; charset=UTF-8 > df = pd.read_csv('nhamcsopd2010.csv' , index_col='PATCODE', low_memory=False) > col_init = list(df.columns.values) > keep_col = ['PATCODE', 'PATWT', 'VDAY', 'VMONTH', 'VYEAR', 'MED1', 'MED2', 'MED3', 'MED4', 'MED5'] > for col in col_init: > if col not in keep_col: > del df[col] I'm no pandas expert, but a couple things come to mind. First, where is your code slow (profile it, even with a few well-placed prints)? If it's in read_csv there might be little you can do unless you load those data repeatedly, and can save a pickled data frame as a caching measure. Second, you loop over columns deciding one by one whether to keep or toss a column. Instead try df = df[keep_col] Third, if deleting those other columns is costly, can you perhaps just ignore them? Can't be more investigative right now. I don't have pandas on Android. :-) Skip --485b397dd70141177804ff71ecad Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

> df =3D pd.read_csv('nhamcsopd2010.csv' , index_= col=3D'PATCODE', low_memory=3DFalse)
> col_init =3D list(df.columns.values)
> keep_col =3D ['PATCODE', 'PATWT', 'VDAY', '= ;VMONTH', 'VYEAR', 'MED1', 'MED2', 'MED3= 9;, 'MED4', 'MED5']
> for col in col_init:
> =C2=A0 =C2=A0 if col not in keep_col:
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 del df[col]

I'm no pandas expert, but a couple things come to mind. = First, where is your code slow (profile it, even with a few well-placed pri= nts)? If it's in read_csv there might be little you can do unless you l= oad those data repeatedly, and can save a pickled data frame as a caching m= easure. Second, you loop over columns deciding one by one whether to keep o= r toss a column. Instead try

df =3D df[keep_col]

Third, if deleting those other columns is costly, can you p= erhaps just ignore them?

Can't be more investigative right now. I don't have = pandas on Android. :-)

Skip

--485b397dd70141177804ff71ecad--