Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #62433
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: How to use the method loadtxt() of numpy neatly? |
| Date | 2013-12-20 11:45 +0100 |
| Organization | None |
| References | <96f12116-29fa-4bce-bd4f-c218ca2ecc65@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4433.1387536344.18130.python-list@python.org> (permalink) |
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?
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
How to use the method loadtxt() of numpy neatly? chao dong <neutronest@gmail.com> - 2013-12-19 21:48 -0800 Re: How to use the method loadtxt() of numpy neatly? rusi <rustompmody@gmail.com> - 2013-12-20 01:45 -0800 Re: How to use the method loadtxt() of numpy neatly? Peter Otten <__peter__@web.de> - 2013-12-20 11:45 +0100
csiph-web