如何从记录列表中计算平均值

时间:2016-06-04 05:48:30

标签: python function python-3.x datetime

所以我正在处理一个Assignment,在计算数据列表中的平均值时遇到了麻烦(数据是从外部.txt文件中读取的)。具体来说,我要做的是从下面的数据列表中读取数据记录。

1-2-2014,Frankton,42305.67,23
12-4-2014,Glenview,21922.22,17
10-2-2015,Glenview,63277.9,32
10-5-2015,Glenview,83290.09,16
11-6-2015,Frankton,81301.82,34
10-10-2015,Frankton,62333.3,40
11-11-2015,Frankton,28998.8,29
1-1-2016,Glenview,51083.5,27
1-3-2016,Glenview,62155.72,42
27-3-2016,Frankton,33075.1,18
2-4-2016,Glenview,61824.7,35

在读取上述数据并将其存储为记录后,例如

1-2-2014 as [rec0] Frankton [rec1] 42305.67 [rec2] 67,23 [rec3]。

我需要提示用户从记录列表中输入商店名称(Frankton或Glenview),然后计算来自用户输入商店的所有记录的总rec2。我也需要对平均值做同样的事情,例如:从用户输入的商店中计算rec [3]的平均值。

目前总交易和总交易打印正确,但平均和平均交易未显示正确的数字。

我在运行此程序时正在寻找的正确输出将是 -

用户输入 Frankton

enter image description here 或者用户输入 Glenview

enter image description here

我目前的代码将发布在下面:

from datetime import datetime

def readdata(filename):
    print('*' * 10, 'Reading Records From ', filename, '*' * 10)
    print('Done.\n\n')
    data = []
    readf = open(filename, 'r')
    for line in readf :
        datestr, branch, dailySale, transactions = line.split(',')
        dateobj = datetime.strptime(datestr, '%d-%m-%Y')
        rec = [dateobj, branch, eval(dailySale), int(transactions)]
        data.append(rec)
readf.close()

return data

def query_branch_sale(records):
    print('*' * 10, 'Querying Total Sales by Branch', '*' * 10)
    branch = input('Enter branch name (Glenview or Frankton): ')
    while branch != 'Glenview' and branch != 'Frankton' :
        print('Invalid. Enter again.')
        branch = input('Enter branch name (Glenview or Frankton): ')
    total = 0
    total_transaction = 0
    for rec in records: 
        if rec[1] == branch:
            total = total + rec[2]
            total_transaction = total_transaction + rec[3]
    average = total/len(records)
    ave_transaction = total_transaction/len(records)
    print(total)
    print(average)
    print(total_transaction)
    print(ave_transaction)

def main():

    records = readdata('data.txt')
    query_branch_sale(records)

main ()

3 个答案:

答案 0 :(得分:0)

在计算平均值时,您不应该除以len(records),因为records包含来自其他分支的交易。您需要添加一个计数器变量,该变量计算所选分支中的事务数,并除以该值。

total = 0
total_transaction = 0
count = 0
for rec in records: 
    if rec[1] == branch:
        total = total + rec[2]
        total_transaction = total_transaction + rec[3]
        count += 1
if count > 0:
    average = total/count
    ave_transaction = total_transaction/count
else:
    average = 0
    ave_transaction = 0

答案 1 :(得分:0)

使用pandas

from pandas import read_csv

def get_sum_and_mean(city):
    with open('records.csv', 'r') as open_file:
        records = read_csv(open_file, header=None)

    records = records.groupby(1)
    return records[2].sum()[city], records[3].mean()[city]

基本上,records.groupby(1)按城市对数据框进行分组,records[2].sum()[city]计算每个城市的第3个总和,并提取您想要的城市,records[3].mean()[city]为第4列的平均值。

答案 2 :(得分:0)

使用pandas可以更方便地使用表格,但如果您不熟悉pandas,则使用line.split(',')拆分列和行很有帮助,然后将其与line[column_number]一起使用获得总结果或平均结果。此外,我更喜欢先分开两个分支,然后计算总数和平均数。

def get_your_money():

    with open(<your-text-file>, 'rb') as f:
        lst = [line.strip() for line in f.readlines()]

    table = [line.split(',') for line in lst]


    while True:
        rec1 = raw_input('Enter branch name (Glenview or Frankton): ')
        if not rec1 in ['Glenview', 'Frankton']:
            print 'Invalid, Enter again.'
            continue
        else:
            rec2 = [float(line[2]) for line in table if line[1] == rec1]
            rec3 = [int(line[3]) for line in table if line[1] == rec1]

            if len(rec2) != 0 and len(rec3) != 0:
                print 'Total sale by {} ${:,.2f}'.format(rec1, sum(rec2))
                print 'Average sale by {} ${:,.2f}'.format(rec1, sum(rec2) / len(rec2))
                print 'Total transactions by {} ${:,}'.format(rec1, sum(rec3))
                print 'Average transactions per day by {} ${:,}'.format(rec1, sum(rec3) / len(rec3))
            else:
                rec2_avg = rec3_avg = 0

            break