Mean, Median, And Mode Of A List Of Values (score) Given A Certain Zip Code For Every Year
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
Solution 1:
Use 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
20174488.088.0885590.090.0906692.592.5907796.096.09620183390.090.0905592.092.0926697.097.09720195596.096.0967790.090.090
Solution 2:
you could use groupby to group the data by date and zipcode and then use the .agg function to apply the mean, median and mode to it. The code would look as follow
groupedData = df.groupby(["DATE","Zip codes"]).agg({"Score" : ["mean","median","mode"]
Post a Comment for "Mean, Median, And Mode Of A List Of Values (score) Given A Certain Zip Code For Every Year"