3

Please go easy, I am a student!

I am unable to export the results of the following code into a CSV. For each loop (coin flip), I need to export the result to a CSV called RESULTS.CSV as a new ROW.

I have tried about four different approaches posted on StackOverflow but been unsuccessful thus far. I am having a difficult time understanding the syntax/logic. I hate asking this and always try to avoid it, but would anyone mind showing me a simple solution so that I can dissect the logic? (rather than referencing to an article, as I have already tried to understand them but am having trouble)

Version: Python 3.5

import random

flipcount=int(input("How many times should I flip a coin?\n###:"))

samples = [ random.randint(1, 2) for i in range(flipcount) ]
heads = samples.count(1)
tails = samples.count(2)

for s in samples:
    msg = 'Heads' if s==1 else 'Tails'
    print(msg)

print ("Heads count=%d, Tails count=%d" % (heads, tails))

Here is a failed attempt... I believe that I have saturated my research on the topic and have greatly confused myself...

import random
import csv

flipcount=int(input("How many times should I flip a coin?\n###:"))


samples = [ random.randint(1, 2) for i in range(flipcount) ]
heads = samples.count(1)
tails = samples.count(2)
writer = csv.writer(w)

for s in samples:
    msg = 'Heads' if s==1 else 'Tails'
    print(msg)
    with open('some.csv', 'w', newline='') as f:
        writer.writerows(msg)

print ("Heads count=%d, Tails count=%d" % (heads, tails))
Tucker
  • 367
  • 1
  • 3
  • 11
  • I think it'll be beneficial to post the failed attempts. Also, take a look at old stackoverflow questions because this problem is not new. – ishaan Jul 07 '16 at 18:20
  • you should post the expected output... we don't know if you wanna write all the tries and/or just the final message... – dot.Py Jul 07 '16 at 18:27

2 Answers2

2
import pandas as pd
import numpy as np
#get the flipcount
flipcount = int(input("How many times should I flip a coin?\n###:"))
samples = np.random.randint(0, 2, size = flipcount)
#create a pandas dataframe
data = pd.DataFrame(["heads" if i == 1 else "Tails" for i in samples])
#create a csv file
data.to_csv("data.csv", index=False, header=False)
hashcode55
  • 5,622
  • 4
  • 27
  • 40
  • Whaaa!... so simple. I have not seen to_csv referenced in the articles I was reading. Thank you very much. This simplified my code tremendously and taught me a few things. Cheers – Tucker Jul 07 '16 at 20:27
  • Also wanted to mention that your version seems to be much faster at the actual coin flip. I can do 1,000,000 flips very quickly. Thanks again for the insight and new tools for me to learn. – Tucker Jul 08 '16 at 20:23
  • You're most welcome :). Numpy is really a fast beast in-fact lol. – hashcode55 Jul 08 '16 at 20:35
  • check this out for why its so fast - http://stackoverflow.com/questions/8385602/why-are-numpy-arrays-so-fast – hashcode55 Jul 08 '16 at 20:38
  • Very interesting read! Thank you. Last but not least, how would I tell pandas to limit a column to 1 million results and to automatically place the next 1 million results into the next column? For example, column one would fill with 1m rows, then the program would keep running but place the next 1m rows into column two, so on and so on? – Tucker Jul 08 '16 at 22:15
  • Why not just split `samples` into lists of 1mil and then use them to create columns using pandas? – hashcode55 Jul 08 '16 at 22:17
0

This will work. Now instead of just printing your results you append them to the list 'data'. Each line of 'data' will contain one of your results. Then you write the list 'data' to a csv file, which will look like you have requested.

import csv 

data = []
for s in samples:
    msg = 'Heads' if s==1 else 'Tails'
    data.append(msg)

with open('flip_file.csv', 'wb') as f:
    writer = csv.writer(f)
    writer.writerows(data)
SAMO
  • 458
  • 1
  • 3
  • 20