给定特定邮政编码的值列表 (SCORE) 的平均值、中位数和众数 [英] Mean, Median, and mode of a list of values (SCORE) given a certain zip code for every year

查看:37
本文介绍了给定特定邮政编码的值列表 (SCORE) 的平均值、中位数和众数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到给定特定邮政编码的每年的均值、中值和众数值,我该如何实现,我已经从 CSV 文件中读取数据并将其转换为 json 文件并将其定义为 DataFrame 我的数据样本不限于下表是更大的

I want to find the mean, median and mode value for each year given a specific ZIP code how can I achieve this, I already read the data from CSV file and convert it to json file and define it as DataFrame my data sample is not limited to the following table it's larger

推荐答案

使用 SciPy.mstats:

In [2295]: df.DATE = pd.to_datetime(df.DATE).dt.year

In [2291]: import scipy.stats.mstats as mstats

In [2313]: def mode(x):
      ...:     return mstats.mode(x, axis=None)[0]
      ...: 

 In [2314]: df.groupby(['DATE', 'ZipCodes']).agg(["mean","median", mode])
Out[2314]: 
              SCORE            
               mean median mode
DATE ZipCodes                  
2017 44        88.0   88.0   88
     55        90.0   90.0   90
     66        92.5   92.5   90
     77        96.0   96.0   96
2018 33        90.0   90.0   90
     55        92.0   92.0   92
     66        97.0   97.0   97
2019 55        96.0   96.0   96
     77        90.0   90.0   90

这篇关于给定特定邮政编码的值列表 (SCORE) 的平均值、中位数和众数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆