-1

I have an existing CSV-file with 4 columns (comma delimited, so all values in one column in excel) enter image description here

How do I write a code to add for example the value '10' to every row (that is the length)
Also, how do I add a string to these rows?

Ideal output would be:
enter image description here


I have tried

a = np.array([2,5])
b = np.array([1000,3000])
c = np.array([10])
d = np.array(["both"])

combined = np.array(list(zip(a,b,c,d)))

-- output of combined: --
array([['2', '1000', '10', 'both']], dtype='<U11')

When I use np.savetxt, I encounter the error message saying:

np.savetxt("testfile.csv", combined, delimiter=",")  

Error:
Mismatch between array dtype ('<U11') and format specifier

Can anyone spot the solution? Do I need to format the np.savetxt? If so, then what to be add? Thanks

1 Answers1

0

Because of the mixed type of the array, you need to specify the formatter when flushing into a file:

Try:

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

Every entry gets casted as a string before writing.

To solve the issue in the comments:

a = np.array([2,5])
b = np.array([1000,3000])
c = np.array([10]*2)
d = np.array(["both"]*2)

This should give you two rows.

Learning is a mess
  • 7,479
  • 7
  • 35
  • 71
  • Thanks that worked but why is only the first element in each list created? Output in csv is now: 2,1000,10,both (which is only one row) – vectorizinglife Jun 25 '19 at 13:43
  • This is because `zip` stops at the shortest length, and your arrays `b` and `c` only have one entry. – Learning is a mess Jun 25 '19 at 13:44
  • How do I tackle that problem? I tried with looping in a range with length of a. Still does not work. I was thinking of adding zeros to match size of other arrays but it feels like a stupid way to do so.. – vectorizinglife Jun 25 '19 at 13:47