Python CSV take square root of data in the field
Question: I have a CSV file containing numbers in the fields,

![enter image description here](http://i.stack.imgur.com/UL3Al.png)

I write a script, trying to take square root of numbers in every field in this
CSV file,

    
    
    import sys, os
    
    import csv, math
    import tempfile
    import shutil
    
    element = []
    filename = 'test.csv'
    with open(filename, 'rb') as f, tempfile.NamedTemporaryFile(mode='wb', delete=False) as g:
        writer = csv.writer(g, delimiter = ',')
        for row in csv.reader(f):
            element = element in row
            row = math.sqrt(element)
            writer.writerow([row])
    
    shutil.move(g.name, filename)
    

But the output is not what I want,

![enter image description here](http://i.stack.imgur.com/gduRZ.png)

What should I edit my script？


Answer: I think you're overcomplicating things. Try something like:

    
    
    import numpy as np
    
    data = np.loadtxt('input.csv', delimiter=',')
    data = np.sqrt(data)
    data = np.where(np.isnan(data), 0, data)
    np.savetxt('output.csv', data, delimiter=',')
    

Note: This assumes you're not reading in irregularly structured data.

