Assume the following data frame:
import pandas as pd
import numpy as np
vals = [1, 2, 3, 4, 5]
df = pd.DataFrame({'val': vals})
df['val'][[0, 3]] = np.nan
Gives:
val
0 NaN
1 2.0
2 3.0
3 NaN
4 5.0
I need to be able to replace NaN values in the val column with a 2D numpy array of zeros. When I do the following:
z = np.zeros((10, 10))
df['val'][df['val'].isnull()] = z
The arrays are converted to scalars of value 0.0:
val
0 0.0
1 2.0
2 3.0
3 0.0
4 5.0
I really need the array to be maintained (in this case, each NaN value - rows 0 and 3 from the original data frame -
should be replaced with a 10x10 array of zeros). I've tried converting to object type first
df = df.astype(object)
df['val'][df['val'].isnull()] = z
With no success. Whhyyyyy
val, do you want the 0th array ofz, and for 3 inval, you want the 3rd item fromz?