[英]pandas DataFrame: replace nan values with median of corresponding column
I have some NaN values in my pd dataframe and I wish to replace them with the median value of the column.我的 pd dataframe 中有一些 NaN 值,我希望用列的中值替换它们。 I am aware that this is very similar to: pandas DataFrame: replace nan values with average of columns我知道这非常类似于: pandas DataFrame: replace nan values with average of columns
However when I try the equivalent:但是,当我尝试等效时:
df = df.fillna(df.median())
I get the following error:我收到以下错误:
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
Any advice is appreciated.任何建议表示赞赏。 Thanks谢谢
Try df.fillna(df.median(axis=1, skipna=True))
试试df.fillna(df.median(axis=1, skipna=True))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.