Lets say I have dataframe with nans in each group like
df = pd.DataFrame({'data':[0,1,2,0,np.nan,2,np.nan,0,1],'group':[1,1,1,2,2,2,3,3,3]})
and a numpy array like
x = np.array([0,1,2])
Now based on groups how to fill the missing values that are in the numpy array I have i.e
df = pd.DataFrame({'data':[0,1,2,0,1,2,2,0,1],'group':[1,1,1,2,2,2,3,3,3]})
data group 0 0 1 1 1 1 2 2 1 3 0 2 4 1 2 5 2 2 6 2 3 7 0 3 8 1 3
Let me explain a bit of how the data should be filled. Consider the group 2. The values of data
are 0,np.nan,2 . The np.nan
is the missing value from the array [0,1,2]
. So the data to be filled inplace of nan is 1
.
For multiple nan values, take a group for example that has data [np.nan,0,np.nan] now the values to be filled in place of nan are 1 and 2. resulting in [1,0,2]
.