2

I am new to programming. I was wondering if anyone can help me create a csv file for the data that I created in python. My data looks like this

import numpy as np
print np.__version__



a = 0.75 + (1.25 - 0.75)*np.random.sample(10000)
print a
b = 8 + (12 - 8)*np.random.sample(10000)
print b
c = -12 + 2*np.random.sample(10000)
print c
x0 = (-b - np.sqrt(b**2 - (4*a*c)))/(2*a)
print x0

The csv file format I am looking to create is 1 column each for a,b,c and x0 (see example below)

enter image description here

Your expert assistance will be highly appreciated

Thanks in advance :-)

Edit 1>> Input code:

import numpy as np
print np.__version__
import csv




a = 0.75 + (1.25 - 0.75)*np.random.sample(10000)
##print a
b = 8 + (12 - 8)*np.random.sample(10000)
##print b
c = -12 + 2*np.random.sample(10000)
##print c
x0 = (-b - np.sqrt(b**2 - (4*a*c)))/(2*a)
##print x0


with open("file.csv",'w') as f:
    f.write('a,b,c,x0\n')
    for val in a,b,c,x0:
        print val
        f.write(','.join(map(str,[a,b,c,x0]))+ '\n')

Output enter image description here

I am being able to generate the data using for loop command (see pic below). The csv format is not outputting as expected.

enter image description here

2 Answers 2

2
with open("file.csv",'w') as f:
  f.write('a,b,c,x0\n')
  --forloop where you generate a,b,c,x0:
    f.write(','.join(map(str,[a,b,c,x0])) + '\n')
Sign up to request clarification or add additional context in comments.

6 Comments

Correct. Also add f.write('a,b,c,x0\n') before for loop
@chefarov indeed, if he wants the headers which it appears he does. Thanks.
Apreciate your assistance on this bravosierra99 and @chefarov. Can you kindly give an example of how to write the for loop to generate data?
@MaroofG Um you might want to spend a little more time googling. It should take you about 2 minutes to figure out how to write a for loop.
@bravosierra99: I am not being able to get the data in 4 separate columns as expected. heres my code: <<<with open("file.csv",'w') as f: f.write('a,b,c,x0\n') for val in a,b,c,x0: print val f.write(','.join(map(str,[a,b,c,x0]))+ '\n')>>>> I am unable to write the code on here in the proper format. This is all new to me, please bear with me.
|
1

There are four range of values you need to iterate over. Every iteration should correspond to each new line written.

Try this:

import numpy as np
print np.__version__
import csv


a_range = 0.75 + (1.25 - 0.75)*np.random.sample(10000)
b_range = 8 + (12 - 8)*np.random.sample(10000)
c_range = -12 + 2*np.random.sample(10000)
x0_range = (-b_range - np.sqrt(b_range**2 - (4*a_range*c_range)))/(2*a_range)


with open("file.csv",'w') as f:
    f.write('a,b,c,x0\n')
    for a,b,c,x0 in zip(a_range, b_range, c_range, x0_range):
        f.write(','.join(map(str,[a,b,c,x0]))+ '\n')

1 Comment

Works exactly as I was expecting!. Appreciate your assistance @chefarov :-)

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.