给定特定邮政编码的值列表 (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