-1

I have datarame and I need to replace the missing value with the given default value from dictionary. the dictionary:

col_to_def_val = {'director_name':'unknown',
'actor_1_name':'unknown', 'actor_2_name':'unknown', 'actor_3_name':'unknown', 'genres':'unknown',
'plot_keywords':'unknown', 'movie_title':'unknown', 'movie_imdb_link':'unknown', 'country':'unknown'}

key is column and value is the default value. any suggestions?

I tried using

df[key]= df[key].fillna(col_to_def_val[key])

but didn't work

Celius Stingher
  • 17,835
  • 6
  • 23
  • 53

1 Answers1

0

All your columns need to be replaced with unknown from what I see. Therefore, you can simply pass:

df['key'] = df['key'].fillna("unknown")

There's no need to do it column-wise for this particular case, where all columns are defaulted to the same value.

Celius Stingher
  • 17,835
  • 6
  • 23
  • 53