首页 文章

Python 3.6没有正确写入csv文件

提问于
浏览
2

我正在为救护车写关于忙碌概率的统计计算等 . 我有两个循环;第一个循环写出.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

    您正在输出 pandas 系列作为字符串 . 请注意,L3是 pandas 系列 . 're confused with what'正在进行时使用 pdb (注意我在第52行添加了 import pdb; pdb.set_trace() ) . pdb 的作用类似于 gdb :它's an interactive debugger. I'我不会在这里查看所有命令,但这里有一些输出:

    (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 .

相关问题