首页 > 解决方案 > Replacing missing value with a certain condition

问题描述

I try to fill the missing values in Critic Score by grouping 'Name'.

games['Rating'] = dupliacted_games.groupby('Name')['Rating'].apply(
                    lambda x: x.fillna(x.value_counts().idxmax() 
                    if x.value_counts().max() >=1 
                    else mode , inplace = False)
                  )
games['Rating'].fillna(
      dupliacted_games['Rating'].value_counts().idxmax(),inplace=True
)

I get this error :

site-packages\pandas\core\generic.py:6287: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy self._update_inplace(new_data)

标签: pythonpandasnumpy

解决方案


It's not an error. You have two ways:

  1. Use pd.DataFrame.copy(deep=True)
  2. Turn off warnings

And always think what you do with your data. Check their size(shape) after groupping, merging e.t.c.


推荐阅读