Python 3.6未正确写入csv文件

2024-09-30 16:20:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为救护车编写关于繁忙概率等的统计计算。我有两个循环;第一个循环写入.csv非常好,我可以使用它并对其运行统计。第二个循环给我带来了麻烦。它与第一个循环几乎相同,但在打印时,它包含奇怪的值,如“Probability0”和dtype:float64”。我不知道它为什么这么做。 我正在Windows机器上运行python3.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
########

Tags: andcsvthelambdanumberisvaluewrite
1条回答
网友
1楼 · 发布于 2024-09-30 16:20:46

您正在将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。你知道吗

嗯。你知道吗

相关问题 更多 >