Python Forum
from numpy array to csv - rounding
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
from numpy array to csv - rounding
#1
Greetings,

I have a 2D numpy array
ARR
storing a bunch of floats. I want to print it row by row to a .csv file, but rounded to 3 significant digits.
Is there a simple way to do this?
This is what I currently have. It prints the array without rounding.
    
with open('./results', 'w') as f:
        writer = csv.writer(f, delimiter=" ")
                 
        for row in ARR:
            writer.writerow(row)  
Cheers!
Reply
#2
Take into Pandas the can use .to_csv with float_format.
import numpy as np
import pandas as pd

arr_rand = np.random.rand(3,4)
df = pd.DataFrame(arr_rand)
>>> df
          0         1         2         3
0  0.440761  0.337663  0.372010  0.892344
1  0.952017  0.747404  0.787696  0.338794
2  0.690075  0.061731  0.541146  0.968825

>>> print(df.to_csv(index=False, header=False, line_terminator='\n', float_format='%.3f').strip())
0.441,0.338,0.372,0.892
0.952,0.747,0.788,0.339
0.690,0.062,0.541,0.969
Reply
#3
The simplest way might be to use pandas.
import numpy as np
import pandas as pd
# Make a float numpy array
data = np.array([[a/b for b in range(1,11)] for a in range(1,11)])
# Write to csv file
df = pd.DataFrame(data)
df.to_csv("test.csv", index=False, header=False)
Output:
1.0,0.5,0.3333333333333333,0.25,0.2,0.16666666666666666,...
Another way to do this is convert all the numbers to strings and write the strings to the csv file.
SchroedingersLion likes this post
Reply
#4
Thank you, you two!

I just found another way. Apparently, numpy offers a rounding function for their arrays, so one can just call
ARR.round(decim)
where
ARR
is a numpy array to be rounded to
decim
signifiant figures.

Then my snippet from above can be used.
Reply
#5
I was solving the wrong problem. I saw CSV and rounding and immediately jumped to the conclusion that you were not happy about losing precision when saving your numpy array to a file. That is the complaint in 99.9% of posts that include "csv" and "rounding" in their topic. Sorry for wasting your time.

Curious though, why do you want to round floats to 3 digits in a csv file? It's not like csv is a presentation format.
SchroedingersLion likes this post
Reply
#6
(Nov-14-2022, 08:25 PM)deanhystad Wrote: Curious though, why do you want to round floats to 3 digits in a csv file? It's not like csv is a presentation format.

I want to lose precision so that the file size does not get unnecessarily large. Saving, say, 5 digits instead of the usual 15 saves a factor 3 then. I work in scientific computing and result files often store millions of floats, so stuff like this pays off.
Reply
#7
Why not use a binary format? Numpy loves binary. A float is 4 bytes. More precision than 6 ascii characters (need a separator). Much faster to read and write.

Or is this some kind of intermediary that has to be readable by programs where numpy is not an option?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 5,914 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  IPython errors for numpy array min/max methods muelaner 1 573 Nov-04-2023, 09:22 PM
Last Post: snippsat
  need help rounding joseph202020 7 1,340 Feb-21-2023, 08:13 PM
Last Post: joseph202020
  Expand the range of a NumPy array? PythonNPC 0 757 Jan-31-2023, 02:41 AM
Last Post: PythonNPC
  Change a numpy array to a dataframe Led_Zeppelin 3 1,128 Jan-26-2023, 09:01 PM
Last Post: deanhystad
  numpy.array has no attribute head Led_Zeppelin 1 1,248 Jul-13-2022, 12:56 AM
Last Post: Led_Zeppelin
  Seeing al the data in a dataframe or numpy.array Led_Zeppelin 1 1,155 Jul-11-2022, 08:54 PM
Last Post: Larz60+
  go over and search in numpy array faster caro 7 1,771 Jun-20-2022, 04:54 PM
Last Post: deanhystad
  Creating a numpy array from specific values of a spreadsheet column JulianZ 0 1,135 Apr-19-2022, 07:36 AM
Last Post: JulianZ
  Trying to understand Numpy/array nabuchadresar 2 1,833 Dec-17-2021, 07:52 AM
Last Post: nabuchadresar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020