1

I have a dataframe and nparray as follows

import pandas as pd
import numpy as np

​dic = {'A': {0: 0.9, 1: "NaN", 2: 1.8, 3: "NaN"}, 
     'C': {0: 0.1, 1: 2.8, 2: -0.1, 3: 0.5}, 
     'B': {0: 0.7, 1: -0.6, 2: -0.1, 3: -0.1},}

df=pd.DataFrame(dic)
print(df)

     A    C    B
0  0.9  0.1  0.7
1  NaN  2.8 -0.6
2  1.8 -0.1 -0.1
3  NaN  0.5 -0.1

a = np.array([1.,2.]) 
a

array([1., 2.])

How would I fill the missing (NaN) values in column A with the values from the nparray? I want to fill the column sequentially based on the order of the array so first array element goes into 1A and second goes into 3A.

1 Answer 1

2

Use numpy.tile to create an array by repeating elements of a

df['A'].replace('NaN', np.nan, inplace = True)

len_tile = math.ceil(df['A'].isnull().sum()/len(a))
non_null_a = np.tile(a, len_tile)

Then use `loc' to fill NaN using the array,

df.loc[df['A'].isnull(), 'A'] = non_null_a

    A       C       B
0   0.9     0.1     0.7
1   1.0     2.8     -0.6
2   1.8     -0.1    -0.1
3   2.0     0.5     -0.1

Note: For the dummy df that you have provided, simply using array a to replace missing values will work. The code I used takes into account situation where there are more NaNs than the length of the array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.