0

I am trying to save my data into csv file and I would like to specify different data types for different columns I am saving (e.g int for id, string for name), how can I do that?

For now with the following method I am able to save all 3 arrays as string.

d = np.column_stack((id, first_name, surname ))
np.savetxt('table.csv', d, delimiter=',', fmt="%s") 

Thank you!

Janna Sherazi
  • 167
  • 1
  • 1
  • 15

1 Answers1

0

If all you're looking for is simply different way to format the output strings, you can look at the documentation (https://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html):

fmt : str or sequence of strs, optional

A single format (%10.5f), a sequence of formats, or a multi-format string, e.g. ‘Iteration %d – %10.5f’, in which case delimiter is ignored.

Just pass fmt as a list of formats, e.g. fmt=("%d", "%s", "%s").

Mikk
  • 804
  • 8
  • 23
  • Since I need to load the data into fake database we're creating, I need to use the column_stack in order to get the desired format. Do you know how could I specify the data format in this case? – Janna Sherazi Jan 18 '17 at 15:01
  • The stacked array is probably a string dtype. `%s` is the only thing that will work. With a structured array he could a mix of `fmt`. – hpaulj Jan 18 '17 at 17:20