Function to modify pandas dataframe

Viewed 24

I have a dataframe of values which I want to add some noise. For that I made this function:

def noise(df, noise):
    return np.random.normal(0, df.std() * noise, df.size)

And call it with:

noise(test_df, 0.15)

However it's giving me a shape missmatch error:

ValueError: shape mismatch: objects cannot be broadcast to a single shape

A dataframe example:

test_df = pd.DataFrame([[1,2],[3,4]], columns=list('AB'), dtype=float) 

I do need my function to have as arguments the dataframe and the noise. What am I doing wrong?

Thank you

1 Answers

df.std() returns a series, one entry for each column. You need to use size=df.shape so you can broadcast that series to the shape, not df.size.

Try with:

def noise(df, noise, seed=None):
    if seed is not None: np.random.seed(seed)
    return np.random.normal(0, df.std() * noise,size=df.shape)

noise(test_df, 0.15, seed=43)

Output:

array([[ 0.05460277, -0.19271801],
       [-0.08029263, -0.11347273]])
Related