I have an existing CSV-file with 4 columns (comma delimited, so all values in one column in excel)
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:
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.