如何从 Python 中的文本文件计算中位数?

我有这个文本文件:


Category;currency;sellerRating;Duration;endDay;ClosePrice;OpenPrice;Competitive?

Music/Movie/Game;US;3249;5;Mon;0,01;0,01;No


Music/Movie/Game;US;3249;5;Mon;0,01;0,01;No


Music/Movie/Game;US;3249;5;Mon;0,01;0,01;No


Music/Movie/Game;US;3249;5;Mon;0,01;0,01;No


Music/Movie/Game;US;3249;5;Mon;0,01;0,01;No


Music/Movie/Game;US;3249;5;Mon;0,01;0,01;No


Music/Movie/Game;US;3249;5;Mon;0,01;0,01;No


Automotive;US;3115;7;Tue;0,01;0,01;No


Automotive;US;3115;7;Tue;0,01;0,01;No


Automotive;US;3115;7;Tue;0,01;0,01;Yes

实际文件中没有空格,否则会显示错误。我想计算每个类别的中位数。因此,例如,我想从 SellerRating 计算中位数。到目前为止我有这个(因为我还需要计算平均值,但我设法做到了):


import csv

import locale

import statistics

from pprint import pprint, pformat


import locale


locale.setlocale(locale.LC_ALL, 'Dutch_Netherlands.1252')


avg_names = 'sellerRating', 'Duration', 'ClosePrice', 'OpenPrice'

averages = {avg_name: 0 for avg_name in avg_names}



num_values = 0

 with open('bijlage.txt', newline='') as bestand:

     csvreader = csv.DictReader(bestand, delimiter=';')

     for row in csvreader:

        num_values += 1

        for avg_name in avg_names:

             averages[avg_name] += locale.atof(row[avg_name])



for avg_name, total in averages.items():

averages[avg_name] = total / num_values


print('raw results:')

pprint(averages)


print() 

print('Averages:')

for avg_name in avg_names:

    rounded = locale.format_string('%.2f', round(averages[avg_name], 2),

                               grouping=True)

    print('  {:<13} {:>10}'.format(avg_name, rounded))

我尝试这样做: import statistics print (statistics.median(averages) 但这不起作用。谁能帮助我,因为我不知道从哪里开始?提前非常感谢!


白衣染霜花
浏览 212回答 3
3回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python