Python 3.6无法正确写入csv文件

时间:2018-09-21 14:34:23

标签: python python-3.x csv

我正在写关于救护车忙碌等可能性的统计计算。我有两个循环;第一个循环将.csv完美地写出,我可以使用它并对其进行统计。第二个循环给我麻烦。它几乎与第一个循环相同,但是在我打印时,它包含奇怪的值,例如“ Probability0”和dtype:float64”。我不知道为什么要这样做。 我在Windows计算机上运行Python 3.6 Anaconda发行版。 代码如下所示。同样,第二个循环正在打印出意外的结果。 我的问题是:如何获得第二个循环以仅打印计算出的值?

# -*- 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
########

1 个答案:

答案 0 :(得分:0)

您正在将pandas系列作为字符串输出。请注意,L3是pandas系列。当您对正在发生的事情感到困惑时,请使用pdb(请注意,我在第52行中添加了import pdb; pdb.set_trace())。 pdb的工作方式类似于gdb:这是一个交互式调试器。我不会在这里讨论所有命令,但是这里是一些输出:

(Pdb) l
 50     L3 = 1 / (sum_probability0 + magic_number1)
 51     
 52     import pdb; pdb.set_trace()
 53     
 54     
 55  -> k = 0
 56     pn_value = 0
 57     # 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
 58     # This loop is writing strange values and I don't know where they come from
 59     # Where is "Probability0" coming from and "dtype: float64"
 60     probabilityN = open('ProbabilityN.csv', 'w')
(Pdb) magic_number1
1.8961732515782912e-06
(Pdb) sum_probability0
Probability0    76879.921926
dtype: float64
(Pdb) L3
Probability0    0.000013
dtype: float64
(Pdb) type(L3)
<class 'pandas.core.series.Series'>

进一步下调,我们可以看到:

(Pdb) n
> /Users/matt/repos/stackoverflow/test2.py(63)<module>()
-> pn_value = L3
(Pdb) pn_value
Probability0    0.000013
dtype: float64
(Pdb) pn_value.values[0]
1.3007297288002737e-05

因此,IIUC,您要输出pn_value.values[0],而不是pn_value

HTH。