繁体   English   中英

如何在CSVPython的同一行中获得2个平均值

[英]How to get 2 averages in the same row in CSVPython

我试图得到最终结果

Please enter a training file name: train.csv

Total Lines Processed: 303

Total Healthy Count: 164

Total Ill Count: 139

Averages of Healthy Patients:

[52.59, 0.56, 2.79, 129.25, 242.64, 0.14, 0.84, 158.38, 0.14, 0.59, 1.41, 0.27, 3.77, 0.00]
Averages of Ill Patients:

[56.63, 0.82, 3.59, 134.57, 251.47, 0.16, 1.17, 139.26, 0.55, 1.57, 1.83, 1.13, 5.80, 2.04]
Seperation Values are:

[54.61, 0.69, 3.19, 131.91, 247.06, 0.15, 1.00, 148.82, 0.34, 1.08, 1.62, 0.70, 4.79, 1.02]

到目前为止,这就是我的代码。 我目前不知道如何获得健康患者和患病患者的平均值。 任何对如何进行的帮助表示赞赏。

import csv
#turn csv files into a list of lists
with open('train.csv') as csvfile:
     reader = csv.reader(csvfile, delimiter=',')
     csv_data = list(reader)


#count the amount of patients with heart problems
Icount = 0
Hcount = 0
for row in csv_data:
    try:
        if (row and int(row[13]) > 0):
            Icount += 1
        if (row and int(row[13]) <=0):
            Hcount += 1
    except IndexError:
        print("could not find the heart diseases status for the row" + str(row))
file = open("train.csv")
numline = len(file.readlines())
HPavg = ( )/(Hcount)
IPavg = ( )/(Icount)

print(numline)
print("Total amount of healthy patients " + str(Icount))
print("Total amount of ill patients " + str(Hcount))
print("Averages of healthy patients " + str(HPavg))
print("Averages of ill patients " + str(IPavg))

因此,您希望总结患病(和健康)患者的价值。

因此,这是您应如何对待患病患者的方法(对健康人适用相同的模式)。 代替Icount += 1考虑将值附加到列表,例如

i_list = []
for row in csv_data:
    if row and int(row[13]) > 0:
        i_list.append(int(row[13]))
    ...

Icount = len(i_list)
IPavg = sum(i_list)/Icount

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM