Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.048 X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; 'slow.': 0.09; 'toss': 0.09; 'cc:addr:python-list': 0.11; 'caching': 0.16; 'col': 0.16; 'columns': 0.16; 'expert,': 0.16; 'skip:d 60': 0.16; 'third,': 0.16; ':-)': 0.16; 'ignore': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'seems': 0.21; 'email addr:gmail.com>': 0.22; '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; 'received:209.85': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'received:209': 0.37; 'pm,': 0.38; 'little': 0.38; 'even': 0.60; 'deleting': 0.60; 'save': 0.62; 'real': 0.63; 'more': 0.64; '30,': 0.65; 'to:addr:gmail.com': 0.65; '8bit%:21': 0.69; 'jul': 0.74; 'android.': 0.84; 'column.': 0.84; 'measure.': 0.84; 'pickled': 0.84 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=zkJk5O1PY8NKSXjBb+poL+X9vE3H3dq4eZNxfSXpWXs=; b=Upkp9w26MMlR+xdaKqMlHeUa/OfsgNvTr+QTyjWbt+sdgt8MdfEYtjgansNtF0Pkd6 0/ANAOSf36ymE+7bKR+EHe17PFtwVw9zDWPU12swJQFhbShd+wxowrNgUKwLRNJ7y5Jd ZfL9W1Io1gtYqj4Z4t1Y7AndYp+oqy5Pp0R/CaRD5umAh7Z+OsZQLEFDLHGB1Ky50fQV nKArIAj2KAu898kWnpOpuAo4ksrTFOOYPuVOChZhnqmNdZHfg9lqUNjsebaUbf9dyZPO MVm7wlJRxB0ynyAOuIfzsDaYAR+Tfy4tT7LTK/jWWOrQro+dnZ8kMP4X96BOTIQPDInj GkqA== X-Gm-Message-State: ALoCoQndykWgLFbfwqdYzbvD95FRx0p8Hpo+/4KwJL08F3MhLvWLQIksN1bZ1P9tU6OTsoXxdyWY X-Received: by 10.182.144.131 with SMTP id sm3mr638264obb.3.1406766529091; Wed, 30 Jul 2014 17:28:49 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Vincent Davis Date: Wed, 30 Jul 2014 18:28:28 -0600 Subject: Re: speed up pandas calculation To: Skip Montanaro Content-Type: multipart/alternative; boundary=089e0158aba23fb24004ff725b5e 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: 118 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1406785998 news.xs4all.nl 2917 [2001:888:2000:d::a6]:52819 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75404 --089e0158aba23fb24004ff725b5e Content-Type: text/plain; charset=UTF-8 On Wed, Jul 30, 2014 at 5:57 PM, Skip Montanaro wrote: > > 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. :-) > So the df = df[keep_col] is not fast but it is not that slow. You made me think of a solution to that part. just slice and copy. The only gotya is that the keep_col have to actually exist keep_col = ['PATCODE', 'PATWT', 'VDAYR', 'VMONTH', 'MED1', 'MED2', 'MED3', 'MED4', 'MED5'] df = df[keep_col] The real slow part seems to be for n in drugs: df[n] = df[['MED1','MED2','MED3','MED4','MED5']].isin([drugs[n]]).any(1) Vincent Davis 720-301-3003 --089e0158aba23fb24004ff725b5e Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

= On Wed, Jul 30, 2014 at 5:57 PM, Skip Montanaro <skip.montanaro@gma= il.com> wrote:

> df =3D pd.read_csv(= 9;nhamcsopd2010.csv' , index_col=3D'PATCODE', low_memory=3DFals= e)
> 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-plac= ed 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 cac= hing measure. Second, you loop over columns deciding one by one whether to = keep or 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. :-)


So the=C2=A0df =3D df[keep_col] is not fast but it is not that slow. You= made me think of a solution to that part. just slice and copy. The only go= tya is that the keep_col have to=C2=A0actually=C2=A0exist
keep_col =3D ['PATCODE', 'PATWT', 'VDAYR', 'VMO= NTH', 'MED1', 'MED2', 'MED3', 'MED4', &= #39;MED5']
df =3D df[keep_col]

The real slow part= seems to be
for n in drugs:
=C2=A0 =C2=A0 df[n] =3D df[['MED1','MED2',&#= 39;MED3','MED4','MED5']].isin([drugs[n]]).any(1)



Vincent Davis
720-301-3003
--089e0158aba23fb24004ff725b5e--