21

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?

2
  • 2
    Use the csv module, csv.writer in particular. Commented Mar 9, 2017 at 7:18
  • Have you tried to use the fmr parameter of savetxt? Commented Mar 9, 2017 at 7:53

3 Answers 3

37
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.

Sign up to request clarification or add additional context in comments.

Comments

2

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()

1 Comment

Thank you but for some reason when using this method the file is created but nothing is in it. Any idea?
-5

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

2 Comments

Is the HTML really necessary? The question was how to not get scientific notation
this answer doesn't add any value to the question. It only show how an HTML table it is converted into CSV but nobody ask for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.