1

I am new to Python, I have a following example that I don't understand

The following is a csv file with some data

%%writefile wood.csv
item,material,number
100,oak,33
110,maple,14
120,oak,7
145,birch,3

Then, the example tries to define a function to convert those trees name above to integers.

tree_to_int = dict(oak = 1,
                   maple=2,
                   birch=3)

def convert(s):
    return tree_to_int.get(s, 0)

The first question is why is there a "0" after "s"? I removed that "0" and get same result.

The last step is to read those data by numpy.array

data = np.genfromtxt('wood.csv',
                     delimiter=',', 
                     dtype=np.int, 
                     names=True,   
                     converters={1:convert}
                    )

I was wondering for the converters argument, what does {1:convert} exact mean? Especially what does number 1 mean in this case?

Bratt Swan
  • 1,068
  • 3
  • 16
  • 28
  • 1
    For your first question, see http://stackoverflow.com/questions/2068349/understanding-get-method-in-python – Warren Weckesser Jan 10 '17 at 00:21
  • 1
    Also see the Python documentation at https://docs.python.org/3/library/stdtypes.html#mapping-types-dict; scroll down to see the description of the [`get` method](https://docs.python.org/3/library/stdtypes.html#dict.get). – Warren Weckesser Jan 10 '17 at 00:24
  • @WarrenWeckesser Okay, that documentation is pretty straightforward. Would you mind answering my second question? – Bratt Swan Jan 10 '17 at 00:39

1 Answers1

1

For the second question, according to the documentation (https://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html), {1:convert} is a dictionary whose keys are column numbers (where the first column is column 0) and whose values are functions that convert the entries in that column.

So in this code, the 1 indicates column one of the csv file, the one with the names of the trees. Including this argument causes numpy to use the convert function to replace the tree names with their corresponding numbers in data.