为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

白衣染霜花 2021-09-25 22:05:09
我有这个文本文件:Category;currency;sellerRating;Duration;endDay;ClosePrice;OpenPrice;Competitive?Music/Movie/Game;US;3249;5;Mon;0,01;0,01;NoMusic/Movie/Game;US;3249;5;Mon;0,01;0,01;NoMusic/Movie/Game;US;3249;5;Mon;0,01;0,01;NoMusic/Movie/Game;US;3249;5;Mon;0,01;0,01;NoMusic/Movie/Game;US;3249;5;Mon;0,01;0,01;NoMusic/Movie/Game;US;3249;5;Mon;0,01;0,01;NoMusic/Movie/Game;US;3249;5;Mon;0,01;0,01;NoAutomotive;US;3115;7;Tue;0,01;0,01;NoAutomotive;US;3115;7;Tue;0,01;0,01;NoAutomotive;US;3115;7;Tue;0,01;0,01;Yes实际文件中没有空格,否则会显示错误。我想计算每个类别的中位数。因此,例如,我想从 SellerRating 计算中位数。到目前为止我有这个(因为我还需要计算平均值,但我设法做到了):import csvimport localeimport statisticsfrom pprint import pprint, pformatimport localelocale.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_valuesprint('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) 但这不起作用。谁能帮助我,因为我不知道从哪里开始?提前非常感谢!
查看完整描述

3 回答

  • 3 回答
  • 0 关注
  • 211 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信