pandas DataFrame:用相应列的中位数替换nan值

时间:2019-10-16 15:59:16

标签: python python-3.x pandas dataframe

我在pd数据框中有一些NaN值,我希望将其替换为列的中值。我知道这非常类似于: pandas DataFrame: replace nan values with average of columns

但是,当我尝试等效时:

df = df.fillna(df.median())

我收到以下错误:


Python\Python37\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)
Python\Python37\site-packages\pandas\core\frame.py:4244: 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
  **kwargs

任何建议都值得赞赏。 谢谢

1 个答案:

答案 0 :(得分:0)

尝试df.fillna(df.median(axis=1, skipna=True))