2

What is the best way to fill missing values in dataframe with items from list? For example:

pd.DataFrame([[1,2,3],[4,5],[7,8],[10,11,12],[13,14]])

        0   1   2
    0   1   2   3
    1   4   5 NaN
    2   7   8 NaN
    3  10  11  12
    4  13  14 NaN

list = [6, 9, 150]

to get some something like this:

       0   1   2
   0   1   2   3
   1   4   5   6
   2   7   8   9
   3  10  11  12
   4  13  14  15
1
  • what if you have two columns both of which have missing values? What order do you use to fill those missing values? Commented Jul 14, 2016 at 23:09

1 Answer 1

2

this is actually a little tricky and a bit of a hack, if you know the column you want to fill the NaN values for then you can construct a df for that column with the indices of the missing values and pass the df to fillna:

In [33]:
fill = pd.DataFrame(index =df.index[df.isnull().any(axis=1)], data= [6, 9, 150],columns=[2])
df.fillna(fill)

Out[33]:
    0   1    2
0   1   2    3
1   4   5    6
2   7   8    9
3  10  11   12
4  13  14  150

You can't pass a dict (my original answer) as the dict key values are the column values to match on and the scalar value will be used for all NaN values for that column which is not what you want:

In [40]:
l=[6, 9, 150]
df.fillna(dict(zip(df.index[df.isnull().any(axis=1)],l)))

Out[40]:
    0   1   2
0   1   2   3
1   4   5   9
2   7   8   9
3  10  11  12
4  13  14   9

You can see that it has replaced all NaNs with 9 as it matched the missing NaN index value of 2 with column 2.

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.