Search code examples
pythonpandasdataframefillna

df.fillna(dict) is not filling NaN values


I have following df:

0                                                                            NaN
1                                                                            NaN
2                                                                            NaN
3                                                                            NaN
4                                                                            NaN
5                                                                            NaN
6                                                                            NaN
7                                                                            NaN
8                                                                            NaN
9                                                                            NaN
10                                                                           NaN
11                                                                           NaN
12    {'A': {'B': '14:00', 'C': '12:30', 'D': '07:30'}}
13                                                                           NaN
14                                                                           NaN
15    {'A': {'B': '14:00', 'C': '12:30', 'D': '08:00'}}
16                                                                           NaN
17    {'A': {'B': '14:00', 'C': '13:30', 'D': '08:00'}}
18    {'A': {'B': '08:00', 'C': '08:00', 'D': '08:00'}}
19                                                                           NaN

I want to fill NaN values of this df with the value at index 12, i.e.

{'A': {'B': '14:00', 'C': '12:30', 'D': '07:30'}}

I tried the following code:

x = {'A': {'B': '14:00', 'C': '12:30', 'D': '07:30'}}
df.fillna(x, inplace= True)

But, it is not filling any NaN values and df remains unchanged. When I put x=1, NaN values are filled with 1. Why is this happening? Any solution to this?


Solution

  • We can still try fillna but need to pass the index dict

    df = df.fillna({12:{'A': {'B': '14:00', 'C': '12:30', 'D': '07:30'}}})