I am writing statistical calculations for ambulances regarding the probability of being busy etc. I have two loops; the first loop writes out to .csv perfectly fine and I am able to consume it and run statistics against it. The second loop is giving me problems. It is nearly identical to the first loop but when I print it contains strange values such as "Probability0" and dtype: float64". I have no idea why it is doing this. I am running Python 3.6 Anaconda distribution on a Windows machine. The code is shown below. Again, it is the second loop that is printing unexpected results. My question is: How can I get the second loop to print only the calculated values?
# -*- coding: utf-8 -*-
# David Kulpanowski
# 15 September 2018
# Python 3.6 Anaconda distribution
# queueing theory calculations for ambulances and fire apparatus
import math
import pandas as pd
# declare variables
number_servers = 40
service_rate = 1.33333333
arrival_rate = 15
lambda_mu = arrival_rate / service_rate
k = 0
# I create a .csv file because when I create an array Python says it cannot append float values in arrays
probability0 = open('c:/temp/Probability0.csv', 'w')
# run the loop and calculate the values for p0
# This loop appears to run correctly
while k <= number_servers:
if(k == 0):
p0_value = 1.0
probability0.write('Probability0\n')
probability0.write(str(p0_value) + '\n')
elif(k == 1):
p0_value = lambda_mu
probability0.write(str(p0_value) + '\n')
elif(k == 2):
p0_value = lambda_mu *lambda_mu / k
probability0.write(str(p0_value) + '\n')
elif( k >= 3 & k <= number_servers):
p0_value = p0_value * lambda_mu / k
probability0.write(str(p0_value) + '\n')
k = k + 1
probability0.close()
# open the .csv and read the contents and display them on screen
df = pd.read_csv('c:/temp/Probability0.csv', sep=',')
print('The probability of 0 is:')
print(df.head(n = 40))
# declare the variables
servers_minus1 = number_servers - 1
magic_number0 = math.factorial(servers_minus1)
sum_probability0 = df.sum()
ls = lambda_mu / number_servers
magic_number1 = (math.pow(lambda_mu, number_servers)) / (magic_number0 * number_servers * (1 - ls))
L3 = 1 / (sum_probability0 + magic_number1)
k = 0
pn_value = 0
# create a .csv file to hold the data. Again, I am not able to make this work with arrays because there is some difficulty appending float values
# This loop is writing strange values and I don't know where they come from
# Where is "Probability0" coming from and "dtype: float64"
probabilityN = open('c:/temp/ProbabilityN.csv', 'w')
while k <= number_servers:
if(k == 0):
pn_value = L3
probabilityN.write('ProbabilityN\n')
probabilityN.write(str(pn_value) + '\n')
elif(k > 0):
pn_value = lambda_mu * pn_value / k
probabilityN.write(str(pn_value) + '\n')
k = k + 1
probabilityN.close()
# open the file and print to screen
df2 = pd.read_csv('c:/temp/ProbabilityN.csv', sep=',')
print('the probability of N is:')
print(df2.head(n=40))
########
# Notice the completely different output between the two csv files even though the loops
# are nearly identical.
# why is Python writing "Probability0" and "dtype: float64"
# By the way, the calculations appear correct when I verify them against a Microsoft Excel file
########