Question

I have a CSV file containing numbers in the fields,

enter image description here

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

What should I edit my script?

Was it helpful?

Solution

You are calculating square root of False, which is the same as square root of 0

>>> element = element in row
False
>>> row = math.sqrt(element)
0

Modify it like this:

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):
        row = [math.sqrt(float(num)) for num in row]
        writer.writerow(row)

OTHER TIPS

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.

The error in your code is because of this line:

element = element in row

This returns a True or a False. And a square root operation on True or False makes no sense.

This is a example run of your code:

>>> element = []
>>> row = [1, 2, 3, 4]
>>> element = element in row
>>> element
False
>>> import math
>>> math.sqrt(element)
0.0

Replace these lines in your code:

    element = element in row
    row = math.sqrt(element)
    writer.writerow([row])

with:

    sqrts = [math.sqrt(x) for x in row]
    writer.writerow(sqrts)

Example:

>>> sqrts = [math.sqrt(x) for x in row]
>>> sqrts
[1.0, 1.4142135623730951, 1.7320508075688772, 2.0]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top