Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.04; 'elif': 0.05; 'great.': 0.07; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'suggest': 0.14; 'language.': 0.14; '236,': 0.16; 'col': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:d 60': 0.16; 'language': 0.16; 'wrote:': 0.18; "skip:' 30": 0.19; 'header:User-Agent:1': 0.23; 'stick': 0.24; 'file.': 0.24; 'question': 0.24; 'asking': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'skip:p 30': 0.29; 'url:mailman': 0.30; 'code': 0.31; "skip:' 10": 0.31; 'file': 0.32; 'probably': 0.32; 'url:python': 0.33; 'but': 0.35; 'curious': 0.36; 'url:listinfo': 0.36; 'url:org': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'files': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'url:mail': 0.40; 'our': 0.64; 'great': 0.65; 'charset:windows-1252': 0.65; 'here': 0.66; 'minutes': 0.67; 'believe': 0.68; 'drugs': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: speed up pandas calculation Date: Thu, 31 Jul 2014 00:29:56 +0100 References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-78-147-185-248.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.0 In-Reply-To: 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: 83 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1406763015 news.xs4all.nl 2847 [2001:888:2000:d::a6]:35307 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75390 On 31/07/2014 00:04, Vincent Davis wrote: > I know this is a general python list and I am asking about pandas but > this question is probably not great for asking on stackoverflow. > I have a list of files (~80 files, ~30,000 rows) I need to process with > my current code it is take minutes for each file. Any suggestions of a > fast way. I am try to stick with pandas for educational purposes. Any > suggestions would be great. If you are curious the can find the data > file I am using below here. > http://www.nber.org/nhamcs/data/nhamcsopd2010.csv > > drugs_current = {'CITALOPRAM': 4332, > 'ESCITALOPRAM': 4812, > 'FLUOXETINE': 236, > 'FLUVOXAMINE': 3804, > 'PAROXETINE': 3157, > 'SERTRALINE': 880, > 'METHYLPHENIDATE': 900, > 'DEXMETHYLPHENIDATE': 4777, > 'AMPHETAMINE-DEXTROAMPHETAMINE': 4035, > 'DEXTROAMPHETAMINE': 804, > 'LISDEXAMFETAMINE': 6663, > 'METHAMPHETAMINE': 805, > 'ATOMOXETINE': 4827, > 'CLONIDINE': 44, > 'GUANFACINE': 717} > > drugs_98_05 = { 'SERTRALINE': 56635, > 'CITALOPRAM': 59829, > 'FLUOXETINE': 80006, > 'PAROXETINE_HCL': 57150, > 'FLUVOXAMINE': 57064, > 'ESCITALOPRAM': 70466, > 'DEXMETHYLPHENIDATE': 70427, > 'METHYLPHENIDATE': 70374, > 'METHAMPHETAMINE': 53485, > 'AMPHETAMINE1': 70257, > 'AMPHETAMINE2': 70258, > 'AMPHETAMINE3': 50265, > 'DEXTROAMPHETAMINE1': 70259, > 'DEXTROAMPHETAMINE2': 70260, > 'DEXTROAMPHETAMINE3': 51665, > 'COMBINATION_PRODUCT': 51380, > 'FIXED_COMBINATION': 51381, > 'ATOMOXETINE': 70687, > 'CLONIDINE1': 51275, > 'CLONIDINE2': 70357, > 'GUANFACINE': 52498 > } > > 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] > if f[-3:] == 'csv' and f[-6:-4] in ('93', '94', '95', '96', '97', '98', > '99', '00', '91', '02', '03', '04', '05'): > drugs = drugs_98_05 > elif f[-3:] == 'csv' and f[-6:-4] in ('06', '08', '09', '10'): > drugs = drugs_current > for n in drugs: > df[n] = > df[['MED1','MED2','MED3','MED4','MED5']].isin([drugs[n]]).any(1) > > > Vincent Davis > 720-301-3003 > > I suggest you ask here https://mail.python.org/mailman/listinfo/pandas-dev which I believe is also gmane.comp.python.pydata. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence