在第二个小数点后停止平均值

时间:2016-04-13 03:37:25

标签: python csv python-3.x

我得到了这个代码,但是我希望得到我打印出来的部分停在小数点后面的2个数字后面(0.00)我不知道如何去做这个可以任何人给一些了解我将用于执行此操作的命令。 这是代码

import csv

#turn csv files into a list of lists
with open('train.csv','rU') as csvfile:
     reader = csv.reader(csvfile)
     csv_data = list(reader)
with open ('train.csv', 'r') as f:
    numline = len(f.readlines())

# Create two lists to handle the patients
# And two more lists to collect the 'sum' of the columns
# The one that needs to hold the sum 'must' have 0 so we 
# can work with them more easily
iList = []
iList_sum = [0,0,0,0,0,0,0,0,0,0,0,0,0]
IPcounter = 0
hList = []
hList_sum = [0,0,0,0,0,0,0,0,0,0,0,0,0]
HPcounter = 0

# Only use one loop to make the process mega faster
for row in csv_data:
    # If row 13 is greater than 0, then place them as unhealthy
    if (row and int(row[13]) > 0):
        # This appends the whole 'line'/'row' for storing :)
        # That's what you want (instead of saving only one cell at a time)
        iList.append(row)
        IPcounter += 1

    # If it failed the initial condition (greater than 0), then row 13
    # is either less than or equal to 0. That's simply the logical outcome
    else:
        hList.append(row)
        HPcounter += 1


# Loop through all the 'rows' of the ill patient 
for ill_data in iList:

    # Loop through the data within each row, and sum them up
    qmark_counter = 0
    for i in range(0,len(ill_data) - 1):
        if ill_data[i] == '?':
            val = 0

        else:
            val = ill_data[i]
        iList_sum[i] += float(val)



# Now repeat the process for healthy patient
# Loop through all the 'rows' of the healthy patient 
for healthy_data in hList:

    # Loop through the data within each row, and sum them up
    for i in range(0,len(healthy_data) - 1):
        hList_sum[i] += float(ill_data[i])


ill_avg = [ ill / len(iList) for ill in iList_sum]
hlt_avg = [ hlt / len(hList) for hlt in hList_sum]



print('Total number of lines ' + str(numline))
print("Total amount of healthy patients " + str(HPcounter))
print("Total amount of ill patients " + str(IPcounter))
print("Averages of healthy patients " + str(hlt_avg))
print("Averages of ill patients " + str(ill_avg))

这是输出

    Total number of lines 303
Total amount of healthy patients 164
Total amount of ill patients 139
Averages of healthy patients [57.0, 0.0, 2.0, 130.0, 236.0, 0.0, 2.0, 174.0, 0.0, 0.0, 2.0, 1.0, 3.0]
Averages of ill patients [56.62589928057554, 0.8201438848920863, 3.5899280575539567, 134.568345323741, 251.4748201438849, 0.15827338129496402, 1.1726618705035972, 139.25899280575538, 0.5467625899280576, 1.5741007194244607, 1.8273381294964028, 1.129496402877698, 5.798561151079137]

2 个答案:

答案 0 :(得分:4)

您可以使用round(number, ndigits),其中ndigits是小数位数。

例如

>>> number = 32.32434354
>>> round(number, 2)
32.32
>>> round(number, 5)
32.32434

另一种方法是使用Python的str.format()

例如

>>> '{:.2f}'.format(number)
'32.32'
>>> '{:.5f}'.format(number)
'32.32434'

其中,

  • {}.format()插入号码的位置。

  • f指定的数字为float

  • .2.5表示将其四舍五入到两位或五位。

答案 1 :(得分:1)

另一种方法是使用字符串.format方法。说明符is here的文档。 {}指定替换将在字符串中发生,f表示替换将是一个浮点数,.2告诉它只显示小数点后的2位数。

print('{:.2f}'.format(32.32434))
# Prints 32.32

打印数字数组,每个数字格式化:

num_list = [1.2345, 2.3456, 3.4567, 4.5678]
print('[' + ', '.join(['{:.2f}'.format(number) for number in num_list]) + ']')
# Prints [1.23, 2.35, 3.46, 4.57]