How to use the method loadtxt() of numpy neatly?
Peter Otten
__peter__ at web.de
Fri Dec 20 05:45:23 EST 2013
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?
More information about the Python-list
mailing list