1

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.

2 Answers2

1

You said you use dataframes, so pandas is is involved right?

Why then not just use it directly?:

df.to_csv('your_file.txt', header=None, index=None, sep='\t', mode='a')

this will format the output according to the datatype the columns had in the dataframe.

Andreas
  • 8,694
  • 3
  • 14
  • 38
  • Thank you Andreas! However, after converting txt, there are several steps. If I changed the savetxt to to_csv, I will need to change a lot of things in my code. But your solution will definetely work! – Bengu Atici Dec 29 '22 at 11:56
1

As numpy.savetxt docs informs you, you might provide sequence of formats e.g. list of strs as fmt, consider following simple example

import numpy as np
import pandas as pd
df = pd.DataFrame([[1,2,3,"Hello"],[4,5,6,"World"]])
fmt = ["%d","%d","%d","%s"]
np.savetxt("test.tsv", df, fmt=fmt, delimiter="\t")

creates test.tsv with following content

1   2   3   Hello
4   5   6   World
Daweo
  • 31,313
  • 3
  • 12
  • 25