我需要用NA替换数据框的某些列。我可以部分执行此操作,但是列中的某些值不会转换为NA。
这是您可以使用的示例数据框:
df = pd.DataFrame({'away_score': {672: 2.0,
673: 1.0,
674: 2.0,
675: 2.0,
676: 1.0,
677: 1.0,
678: 2.0,
679: 1.0,
680: 1.0,
681: 2.0},
'home_score': {672: 2.0,
673: 2.0,
674: 3.0,
675: 0.0,
676: 0.0,
677: 2.0,
678: 2.0,
679: 1.0,
680: 2.0,
681: 2.0},
'match_id': {672: 273236,
673: 273234,
674: 273239,
675: 273231,
676: 273232,
677: 273238,
678: 273237,
679: 273240,
680: 273233,
681: 273235},
'match_status': {672: 'Finished',
673: 'Finished',
674: 'Finished',
675: 'Finished',
676: 'Finished',
677: 'Finished',
678: 'Finished',
679: 'Finished',
680: 'Finished',
681: 'Finished'}})
到目前为止,我的代码:
columns_to_fillna = ["match_status","home_score","away_score"]
for column in columns_to_fillna:
df[column] = df[column].apply(lambda row: df[column].replace(row,np.nan))
这以某种方式更改了home_score和away_score列的某些行,并且某些行保持不变。这里可能是什么问题?
答案 0 :(得分:1)
>>> columns_to_fillna = ["match_status","home_score","away_score"]
>>> df.replace(df[columns_to_fillna], np.nan)
away_score home_score match_id match_status
672 NaN NaN 273236 NaN
673 NaN NaN 273234 NaN
674 NaN NaN 273239 NaN
675 NaN NaN 273231 NaN
676 NaN NaN 273232 NaN
677 NaN NaN 273238 NaN
678 NaN NaN 273237 NaN
679 NaN NaN 273240 NaN
680 NaN NaN 273233 NaN
681 NaN NaN 273235 NaN