Skip to content
Advertisement

How to write an object typed array into csv file with NumPy?

I have two numpy arrays(A, B) and 2 scalar values(C,D) that I want to store in a csv file. I know how to write a single numpy array in it:

A = np.array(...)
np.savetxt('path/to/file/filename.csv', A, delimiter = ",")

I want the first two columns of my csv-file to contain the 2 arrays A and B and then have the 2 scalar values C and D as the first entry of columns 3 and 4.

Advertisement

Answer

I think you must use iterations or other libraries e.g. Pandas to do this, because each of the columns will have a different size (in this example we will handle unequal sizes of A and B). So the saved array must be specified as object type if you want to do this by np.savetxt. If you use object typed array to be saved by np.savetxt, it will just fill the first row of the csv file:

A = np.array([2, 4.1, 5], dtype=np.float64)
B = np.array([2, 7, 9, 1], dtype=np.float64)
C = 1
D = 7

Output = np.zeros((1, 4), dtype=object)
Output[0, :] = A, B, C, D

np.savetxt('Output.csv', Output, delimiter=",", fmt='%s')

which will be as:
enter image description here

I don’t think if it can be good handled just by NumPy, and will be better to try by other libraries such as Pandas or … or use iterations to open the csv file and import in it. But if you want to use NumPy for doing so (just for figuring out how to do this work by NumPy), it can be achieved by padding to equalize the arrays’ sizes in indirect way. For this aim, we must find maximum length of the A and B to pad the arrays to that length. I filled the padded indices by np.nan in this example and then removed it for the output:

max_len = max(A.shape[0], B.shape[0])

A_pad = np.pad(A, (0, max_len - A.shape[0]), constant_values=(np.nan,))
B_pad = np.pad(B, (0, max_len - B.shape[0]), constant_values=(np.nan,))
C_pad = np.pad(np.array([C], dtype=np.float64), (0, max_len - 1), constant_values=(np.nan,))
D_pad = np.pad(np.array([D], dtype=np.float64), (0, max_len - 1), constant_values=(np.nan,))

Output = np.array([A_pad, B_pad, C_pad, D_pad]).T
Output = Output.astype(str)
Output[Output == 'nan'] = ''

np.savetxt('Output.csv', Output, delimiter=",", fmt="%s")

enter image description here

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement