I have a dataset based on a 5-point Likert scale. I want to transform each cell into a normal distribution value with pregiven mean and std. My code for now is as follows.
import random
Mu={1:0.021,2:0.146,3:0.375,4:0.625,5:0.979}
std={1:0.021,2:0.104,3:0.125,4:0.125,5:0.021}
#defining the random dictionary
rnd={1:random.normalvariate(Mu[1], std[1]),
2:random.normalvariate(Mu[2], std[2]),
3:random.normalvariate(Mu[3], std[3]),
4:random.normalvariate(Mu[4], std[4]),
5:random.normalvariate(Mu[5], std[5])}
raw_data_rnd=raw_data.copy()
for col in raw_data_rnd.columns:
raw_data_rnd[col].mask(raw_data_rnd[col]==1,random.normalvariate(Mu[1],std[1]),inplace=True)
raw_data_rnd[col].mask(raw_data_rnd[col]==2,random.normalvariate(Mu[2],std[2]),inplace=True)
raw_data_rnd[col].mask(raw_data_rnd[col]==3,random.normalvariate(Mu[3],std[3]),inplace=True)
raw_data_rnd[col].mask(raw_data_rnd[col]==4,random.normalvariate(Mu[4],std[4]),inplace=True)
raw_data_rnd[col].mask(raw_data_rnd[col]==5,random.normalvariate(Mu[5],std[5]),inplace=True)
raw_data_rnd
The code is working. However, it gives the same value for every cell where the condition is true. What I need is for the code to somehow loop the random value assignment and put a different value in each cell. In other words, for instance every time the dataframe has the value of 1, I need the code to assign a new random value.
Can anyone help, please?
I have tried several methods. However, I am still hitting deadends.
Aucun commentaire:
Enregistrer un commentaire