I was able to calculate the mean, min and max of A:
import numpy as np
A = ['33.33', '33.33', '33.33', '33.37']
NA = np.asarray(A)
NA = NA.astype(float)
AVG = np.mean(NA, axis=0)
MN = np.min(NA, axis=0)
MX = np.max(NA, axis=0)
print AVG, MN, MX
What is the easiest way to save those results to a csv documento using python? I dont have one so it needs to be created.
If I use this:
np.savetxt('datasave.csv', (AVG,MN,MX), delimiter=',')
It will show as scientific notation in csv. How do I not get that but float?
In [153]: print(AVG, MN, MX)
33.34 33.33 33.37
The default write:
In [154]: np.savetxt('test.txt',(AVG, MN, MX), delimiter=',')
In [155]: cat test.txt
3.333999999999999631e+01
3.332999999999999829e+01
3.336999999999999744e+01
write with a custom fmt
:
In [156]: np.savetxt('test.txt',(AVG, MN, MX), delimiter=',', fmt='%f')
In [157]: cat test.txt
33.340000
33.330000
33.370000
Or if you want values on one line, make it a 2d array (or equivalent with extra []), so it writes one row per line:
In [160]: np.savetxt('test.txt',[[AVG, MN, MX]], delimiter=',', fmt='%f')
In [161]: cat test.txt
33.340000,33.330000,33.370000
There are other parameters that you can experiment with.
You can use the CSV module
import csv
f = open('New file.csv',"wb")
writer = csv.writer(f)
for row in [AVG, MN, MX]:
writer.writerow(row)
f.close()
A CSV or Comma Separated Value file is precisely just that a file that separates info with commas.
For example: this info will end up like
<table border="1"> <thead> <tr> <th>Name</th> <th>Age</th> <th>Gender</th> </tr> </thead> <tbody> <tr> <td>Luis</td> <td>45</td> <td>M</td> </tr> <tr> <td>Anna</td> <td>30</td> <td>F</td> </tr> <tr> <td>Brian</td> <td>28</td> <td>M</td> </tr> </tbody> </table>
Name,Age,Gender
Luis,45,M
Anna,30,F
Brian,28,M
Python has already a built-in module for this, the csv module, you can get some documentation here: https://docs.python.org/3/library/csv.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.