I want to translate the following R code:
k =5
Ret = as.data.frame(sapply(1:k, function(i) rnorm(k) ) )
deltas=NULL
for (i in 1:k) {
y = Ret[,i]
mvalue = mean(y)
minvec = NULL
for (j in 1:length(y))
{
minvechelp = min(0,(y[j]-mvalue))
minvec[j] = minvechelp
}
delta=sqrt(sum(minvec^2)/length(y))
deltas = cbind(deltas,delta)
}
deltas
Which implements a collide mean of a data frame and then takes the length of the data frame to calculate a parameter delta which contains the min calculation but I think the df has been converted to vector.
In Python my effort is:
k= 5
A = pd.DataFrame(
[np.random.randn(k) for i in range(k)]
)
A.size
deltas = []
for i in range(0,k):
y = Ret.iloc[:,i]
mvalue = np.mean(y)
minvec = []
for j in range(0,y.size):
minvechelp = min(0,(y[j]-mvalue))
minvec[j] = minvechelp
delta=np.sqrt(sum(minvec**2)/y.size())
deltas = np.hstack((deltas,delta))
deltas
but I receive an error
KeyError: 0
Where is my mistake and how can I fix it? Any help?