Pandas fillna method does not work inplace

心已入冬 提交于 2019-12-11 04:26:20

问题


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]

It resulted to this :

      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 

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)

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

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


回答1:


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


来源:https://stackoverflow.com/questions/46377263/pandas-fillna-method-does-not-work-inplace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!