I have a dataframe that needs be txt file. So, I use np.savetxt
method. However, dataframe consists of four columns which are string, string, string and integer, respectively.
np.savetxt
takes fmt
and delimiter
parameters. In official document, it is said that multi-format string, e.g. ‘Iteration %d – %10.5f’, in which case delimiter is ignored
How can I use fmt
for both specifying the columns' data types and for tab delimiter?
fmt = ('%s %s %s %d')
np.savetxt("test.txt", df, fmt=fmt)
Sample data:
DTL 3E046ZM/A 190198848376 0
DTL 3E047ZM/A 190198848406 0
DTL 3E309ZM/A 190198937339 0
I tried ('%s \t %s \t %s \t %d')
but it didn't work.