pandas fillna方法不适用于原位 [英] Pandas fillna method does not work inplace

查看:106
本文介绍了 pandas fillna方法不适用于原位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框问题数据,在某些单元格中具有NaN值.我运行了以下代码.

I have a dataframe problem_data which has NaN values in some cells. I ran the following code.

problem_data[problem_data['level_type'] == 5.0]

结果是这样:

      problem_id    level_type  points  tags 
5    prob_1479    5.0        NaN    NaN 
31   prob_2092    5.0        NaN    NaN 
38   prob_4395    5.0        NaN    combinatorics,constructive algorithms,dfs 
43   prob_5653    5.0        NaN    NaN 
48   prob_2735    5.0       2750.0  NaN 
52   prob_1054    5.0       2000.0  combinatorics,dp
64   prob_2610    5.0        NaN    NaN
65   prob_1649    5.0        NaN    NaN
70   prob_4675    5.0        NaN    dp,games
74   prob_445     5.0        NaN    NaN
81   prob_6481    5.0       2500.0  combinatorics,dp,implementation,number theory
134  prob_2964    5.0       2500.0  games
161  prob_948     5.0       2000.0  dp,games
182  prob_642     5.0        NaN    NaN 

然后,我运行以下命令以填充点"列的NaN.

Then, I ran the following command to fill the NaN of 'points' column.

problem_data.loc[problem_data['level_type'] == 5.0 , 'points'].fillna(value=2500, inplace=True)

当我再次运行problem_data[problem_data['level_type'] == 5.0]时,输出与以前相同.

When, I ran problem_data[problem_data['level_type'] == 5.0] again, the output was same as before.

您能说出为什么fillna()在这里不起作用吗?我该怎么做才能纠正它?

Can you tell why fillna() didn't work here? What can I do to correct it?

推荐答案

fillna不适用于数据帧子切片.您需要:

fillna does not work inplace on dataframe sub-slices. You'll want:

mask = problem_data['level_type'] == 5.0
problem_data.loc[mask, 'points'] = problem_data.loc[mask, 'points'].fillna(value=2500)

problem_data.loc[mask, 'points'] 
5      2500.0
31     2500.0
38     2500.0
43     2500.0
48     2750.0
52     2000.0
64     2500.0
65     2500.0
70     2500.0
74     2500.0
81     2500.0
134    2500.0
161    2000.0
182    2500.0
Name: points, dtype: float64

这篇关于 pandas fillna方法不适用于原位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆