簡體   English   中英

在 Pandas 中使用 fillna() 和 lambda function 替換 NaN 值

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

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. 我嘗試了兩種選擇:

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'

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

我的數據框:

游戲數據

在此處輸入圖像描述

流派分數

在此處輸入圖像描述

我會很高興得到任何幫助!

您也可以直接使用user_score_by_genre映射fillna()

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)

順便說一句,如果games_data.user_score永遠不會偏離genre_score值,您可以跳過fillna()並直接分配給games_data.user_score

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

Pandas 內置的Series.where也可以使用,而且更加簡潔:

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

使用numpy.where

import numpy as np

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

我在這里找到了解決方案的一部分

我使用 series.map:

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

之后我使用@MayankPorwal 回答:

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

我不確定這是最好的方法,但它對我有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM