简体   繁体   中英

Fill missing values in a dataframe using numpy.ndarray

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM