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
#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


Messages In This Thread
RE: from numpy array to csv - rounding - by snippsat - Nov-14-2022, 06:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python code to calculate mean of an array of numbers using numpy viren 3 170 May-29-2024, 04:49 PM
Last Post: Gribouillis
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 6,098 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  IPython errors for numpy array min/max methods muelaner 1 661 Nov-04-2023, 09:22 PM
Last Post: snippsat
  need help rounding joseph202020 7 1,442 Feb-21-2023, 08:13 PM
Last Post: joseph202020
  Expand the range of a NumPy array? PythonNPC 0 824 Jan-31-2023, 02:41 AM
Last Post: PythonNPC
  Change a numpy array to a dataframe Led_Zeppelin 3 1,204 Jan-26-2023, 09:01 PM
Last Post: deanhystad
  numpy.array has no attribute head Led_Zeppelin 1 1,357 Jul-13-2022, 12:56 AM
Last Post: Led_Zeppelin
  Seeing al the data in a dataframe or numpy.array Led_Zeppelin 1 1,225 Jul-11-2022, 08:54 PM
Last Post: Larz60+
  go over and search in numpy array faster caro 7 1,919 Jun-20-2022, 04:54 PM
Last Post: deanhystad
  Creating a numpy array from specific values of a spreadsheet column JulianZ 0 1,232 Apr-19-2022, 07:36 AM
Last Post: JulianZ

Forum Jump:

User Panel Messages

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