Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!ecngs!feeder2.ecngs.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'column': 0.07; 'float': 0.07; 'string': 0.09; '"__main__":': 0.09; '-1)': 0.09; '__name__': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'style.': 0.09; 'subject:method': 0.09; 'subject:How': 0.10; 'def': 0.12; '""")': 0.16; '"a"': 0.16; '"b"': 0.16; '"b":': 0.16; 'columns': 0.16; 'csv': 0.16; 'csv,': 0.16; 'dict': 0.16; 'enum': 0.16; 'everybody.': 0.16; 'help?': 0.16; 'numpy': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.18; 'file,': 0.19; 'import': 0.22; 'print': 0.22; 'load': 0.23; 'header:User-Agent:1': 0.23; 'skip': 0.24; 'question': 0.24; 'header:X-Complaints-To:1': 0.27; 'style': 0.33; 'third': 0.33; 'skip:_ 10': 0.34; 'subject:the': 0.34; 'problem.': 0.35; 'convert': 0.35; 'but': 0.35; 'subject:?': 0.36; 'hi,': 0.36; 'example,': 0.37; 'skip:o 20': 0.38; 'thank': 0.38; 'to:addr :python-list': 0.38; 'little': 0.38; 'anything': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'solve': 0.60; 'new': 0.61; 'you.': 0.62; 'skip:n 10': 0.64; 'face': 0.64; 'converters': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: How to use the method loadtxt() of numpy neatly? Date: Fri, 20 Dec 2013 11:45:23 +0100 Organization: None References: <96f12116-29fa-4bce-bd4f-c218ca2ecc65@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50848dc7.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 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: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1387536344 news.xs4all.nl 2906 [2001:888:2000:d::a6]:45672 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:62433 chao dong wrote: > HI, everybody. When I try to use numpy to deal with my dataset in the > style of csv, I face a little problem. > > In my dataset of the csv file, some columns are string that can not > convert to float easily. Some of them can ignore, but other columns I > need to change the data to a enum style. > > for example, one column just contain three kinds : S,Q,C. Each of them > can declare one meaning, so I must convert them to a dict just like > {1,2,3} > > Now the question is, when I use numpy.loadtxt, I must do all things > above in just one line and one fuction. So as a new user in numpy, I > don't know how to solve it. > > Thank you. Here's a standalone demo: import numpy _lookup={"A": 1, "B": 2} def convert(x): return _lookup.get(x, -1) converters = { 0: convert, # in column 0 convert "A" --> 1, "B" --> 2, # anything else to -1 } if __name__ == "__main__": # generate csv with open("tmp_sample.csv", "wb") as f: f.write("""\ A,1,this,67.8 B,2,should,56.7 C,3,be,34.5 A,4,skipped,12.3 """) # load csv a = numpy.loadtxt( "tmp_sample.csv", converters=converters, delimiter=",", usecols=(0, 1, 3) # skip third column ) print a Does that help?