mean

Find Average of Every Three Columns in Pandas dataframe

。_饼干妹妹 提交于 2019-11-26 17:04:52
问题 I am new to Python and Pandas. I have a panda dataframe with monthly columns ranging from 2000 (2000-01) to 2016 (2016-06). I want to find the average of every three months and assign it to a new quarterly column (2000q1). I know I can do the following: df['2000q1'] = df[['2000-01', '2000-02', '2000-03']].mean(axis=1) df['2000q2'] = df[['2000-04', '2000-05', '2000-06']].mean(axis=1) . . . df['2016-02'] = df[['2016-04', '2016-05', '2016-06']].mean(axis=1) But, this is very tedious. I

calculating mean for every n values from a vector

被刻印的时光 ゝ 提交于 2019-11-26 16:56:57
问题 So lets say I have a vector a <- rnorm(6000) I want to calculate the mean of the 1st value to the 60th, then again calculate the mean for the 61st value to the 120th and so fourth. So basically I want to calculate the mean for every 60th values giving me 100 means from that vector. I know I can do a for loop but I'd like to know if there is a better way to do this? 回答1: I would use colMeans(matrix(a, 60)) .colMeans(a, 60, length(a) / 60) # more efficient (without reshaping to matrix)

Reading multiple files and calculating mean based on user input

a 夏天 提交于 2019-11-26 16:36:38
I am trying to write a function in R which takes 3 inputs: Directory pollutant id I have a directory on my computer full of CSV's files i.e. over 300. What this function would do is shown in the below prototype: pollutantmean <- function(directory, pollutant, id = 1:332) { ## 'directory' is a character vector of length 1 indicating ## the location of the CSV files ## 'pollutant' is a character vector of length 1 indicating ## the name of the pollutant for which we will calculate the ## mean; either "sulfate" or "nitrate". ## 'id' is an integer vector indicating the monitor ID numbers ## to be

Why does isNaN(“ ”) equal false

邮差的信 提交于 2019-11-26 15:49:41
I have a quick question (I hope!). In JS, why does isNaN(" ") evaluate to false, but isNaN(" x") evaluate to true? I'm performing numerical operations on a text input field, and am checking if the field is null, "", or NaN. When someone types a handful of spaces into the field, my validation fails on all three, and I'm confused as to why it gets past the isNAN check. Thanks! JavaScript interprets an empty string as a 0, which then fails the isNAN test. You can use parseInt on the string first which won't convert the empty string to 0. The result should then fail isNAN. Nick Berardi You may

MATLAB find and apply function to values of repeated indices

十年热恋 提交于 2019-11-26 14:55:56
问题 I have a 352x11 matrix, indexed by column 1 with 10 data points. Some of the index values are repeated. I'd like to find the repeated indices and calculate the mean data points for the repeated trials (avoiding loops, if possible). For example, x = 26 77.5700 17.9735 32.7200 27 40.5887 16.6100 31.5800 28 60.4734 18.5397 33.6200 28 35.6484 27.2000 54.8000 29 95.3448 19.0000 37.7300 30 82.7273 30.4394 39.1400 to end up with: ans = 26 77.5700 17.9735 32.7200 27 40.5887 16.6100 31.5800 28 48.0609

Get the means of sub groups of means in R

删除回忆录丶 提交于 2019-11-26 14:50:43
问题 I'm a newbie of R and I don't know how to get R calculate the means of a subgroups of means which are the means of a subgroup themselves. I'll explain clearer. I have a data frame like this: GROUP WORD WLN 1 1 4 1 1 3 1 1 3 1 2 2 1 2 2 1 2 3 2 3 1 2 3 1 2 3 2 2 4 1 2 4 1 2 4 1 ... ... ... but the real one has a total of 5 groups and 25 words (5 words each group; every word has being assigned a number from 1 to 4 by 5 subjects...). I need to get the means of WLN for every word and I can do

in R, how to calculate mean of all column, by group?

随声附和 提交于 2019-11-26 14:39:14
问题 I need to get the mean of all columns of a large data set using R, grouped by 2 variables. Lets try it with mtcars: library(dplyr) g_mtcars <- group_by(mtcars, cyl, gear) summarise(g_mtcars, mean (hp)) # Source: local data frame [8 x 3] # Groups: cyl [?] # # cyl gear `mean(hp)` # <dbl> <dbl> <dbl> # 1 4 3 97.0000 # 2 4 4 76.0000 # 3 4 5 102.0000 # 4 6 3 107.5000 # 5 6 4 116.5000 # 6 6 5 175.0000 # 7 8 3 194.1667 # 8 8 5 299.5000 It works for "hp", but I need to get the mean for every other

统计

守給你的承諾、 提交于 2019-11-26 14:34:24
mean 平均数 median 中位数 mode 众数 prctile 四分位数,也称四分位点,是指在统计学中把所有数值由小到大排列并分成四等份,处于三个分割点位置的数值 >> load stockreturns >> x4=stocks(:,4) x4 = 2.9914 2.6204 -0.3624 2.2094 -2.4079 2.9277 -1.0281 -3.3567 1.9482 2.3593 3.5894 -0.8645 -2.1599 -3.8850 -0.3494 -0.8930 1.7893 -0.7141 -1.2013 -3.3592 0.6673 3.7411 2.1308 2.6651 0.9303 -1.6121 0.4302 2.4841 -1.9898 4.5561 2.5903 -0.1640 1.3072 -0.4932 -4.1415 -0.1348 3.2650 -3.1646 -0.6340 -0.7101 -4.3216 0.6100 -1.5280 0.2114 -1.4884 0.9389 1.1603 -0.0880 -2.7591 3.3299 -0.2885 2.2003 -4.3053 0.4916 0.4022 -0.9178 -5.8764 -5.4219 2.9621 -3.1092 2.1127 0.8175 -2

Calculating arithmetic mean (one type of average) in Python

一笑奈何 提交于 2019-11-26 12:42:20
Is there a built-in or standard library method in Python to calculate the arithmetic mean (one type of average) of a list of numbers? NPE I am not aware of anything in the standard library. However, you could use something like: def mean(numbers): return float(sum(numbers)) / max(len(numbers), 1) >>> mean([1,2,3,4]) 2.5 >>> mean([]) 0.0 In numpy, there's numpy.mean() . NumPy has a numpy.mean which is an arithmetic mean. Usage is as simple as this: >>> import numpy >>> a = [1, 2, 4] >>> numpy.mean(a) 2.3333333333333335 kirbyfan64sos Use statistics.mean : import statistics print(statistics.mean(

Calculate mean and standard deviation from a vector of samples in C++ using Boost

我是研究僧i 提交于 2019-11-26 11:50:45
问题 Is there a way to calculate mean and standard deviation for a vector containing samples using Boost? Or do I have to create an accumulator and feed the vector into it? 回答1: Using accumulators is the way to compute means and standard deviations in Boost. accumulator_set<double, stats<tag::variance> > acc; for_each(a_vec.begin(), a_vec.end(), bind<void>(ref(acc), _1)); cout << mean(acc) << endl; cout << sqrt(variance(acc)) << endl; 回答2: I don't know if Boost has more specific functions, but you