Use fillna() and lambda function in Pandas to replace NaN values

Viewed 2205

I'm trying to write fillna() or a lambda function in Pandas that checks if 'user_score' column is a NaN and if so, uses column's data from another DataFrame. I tried two options:

games_data['user_score'].fillna(
    genre_score[games_data['genre']]['user_score']
    if np.isnan(games_data['user_score'])
    else games_data['user_score'],
    inplace = True
)

# but here is 'ValueError: The truth value of a Series is ambiguous'

and

games_data['user_score'] = games_data.apply(
    lambda row: 
    genre_score[row['genre']]['user_score'] 
    if np.isnan(row['user_score'])
    else row['user_score'],
    axis=1
)

# but here is 'KeyError' with another column from games_data

My dataframes:

games_data

enter image description here

genre_score

enter image description here

I will be glad for any help!

3 Answers

You can also fillna() directly with the user_score_by_genre mapping:

user_score_by_genre = games_data.genre.map(genre_score.user_score)
games_data.user_score = games_data.user_score.fillna(user_score_by_genre)

BTW if games_data.user_score will never deviate from the genre_score values, you can skip the fillna() and just assign directly to games_data.user_score:

games_data.user_score = games_data.genre.map(genre_score.user_score)

Pandas' built-in Series.where also works and is a bit more concise:

df1.user_score.where(df1.user_score.isna(), df2.user_score, inplace=True)

Use numpy.where:

import numpy as np

df1['user_score'] = np.where(df1['user_score'].isna(), df2['user_score'], df1['user_score'])

I found the part of the solution here

I use series.map:

user_score_by_genre = games_data['genre'].map(genre_score['user_score'])

And after that I use @MayankPorwal answer:

games_data['user_score'] = np.where(games_data['user_score'].isna(), user_score_by_genre, games_data['user_score'])

I'm not sure that it is the best way but it works for me.

Related