Home > database >  How to Numpy savetxt with thousand seperater?
How to Numpy savetxt with thousand seperater?

Time:10-18

for example, I would like to use numpy savetxt fmt to save 12033000.000 as 12,033,000.000? how do I set fmt parameter?

CodePudding user response:

I can define a utility function using the newer format style:

In [22]: def foo(n):
    ...:     return '{:,}'.format(n)
    ...:     

In [23]: foo(12345.67)
Out[23]: '12,345.67'

In [24]: arr = np.random.randint(10000,100000,(3,4))

In [25]: arr
Out[25]: 
array([[65585, 61527, 73977, 89861],
       [10232, 30367, 80912, 41220],
       [42430, 69606, 34136, 33445]])

and apply it to an array with:

In [26]: new = np.vectorize(foo)(arr)

In [27]: new
Out[27]: 
array([['65,585', '61,527', '73,977', '89,861'],
       ['10,232', '30,367', '80,912', '41,220'],
       ['42,430', '69,606', '34,136', '33,445']], dtype='<U6')

Then use savetxt to write the strings:

In [28]: np.savetxt('test.txt',_,fmt='%s', delimiter=';')

In [29]: !more test.txt
65,585;61,527;73,977;89,861
10,232;30,367;80,912;41,220
42,430;69,606;34,136;33,445

genfromtxt can't handle it (unless there's some parameter I'm not aware of):

In [30]: np.genfromtxt('test.txt', delimiter=';')
Out[30]: 
array([[nan, nan, nan, nan],
       [nan, nan, nan, nan],
       [nan, nan, nan, nan]])

but pandas can:

In [31]: pd.read_csv('test.txt',delimiter=';',thousands=',', header=None)
Out[31]: 
       0      1      2      3
0  65585  61527  73977  89861
1  10232  30367  80912  41220
2  42430  69606  34136  33445

edit

Using '{:_}'.format(n) in foo, produces a loadable file

In [32]: more test.txt
57_051;26_495;68_173;48_213
78_997;49_659;28_987;73_518
79_670;89_775;24_046;48_287

In [33]: np.genfromtxt('test.txt', delimiter=';')
Out[33]: 
array([[57051., 26495., 68173., 48213.],
       [78997., 49659., 28987., 73518.],
       [79670., 89775., 24046., 48287.]])
  • Related