简体   繁体   English

pandas DataFrame:将nan值替换为对应列的中值

[英]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.

相关问题 Pandas Dataframe 用 B 列的值替换 A 列的 NaN 值 - Pandas Dataframe replace NaN values of column A with values from column B 熊猫:如果数据框列为“ NaN”,则替换该列 - Pandas: Replace dataframe column if it is `NaN` 根据条件,用相应的列名替换 pandas 数据框中的特定值, - Replace specific values in pandas dataframe with the corresponding column name, based on a condition, 根据 pandas dataframe 中的相邻列将 NaN 值替换为特定文本 - Replace NaN values with specific text based on adjacent column in pandas dataframe pandas Dataframe基于键列,将NaN值替换为以前的值 - pandas Dataframe Replace NaN values with with previous value based on a key column 如何在 Pandas 数据框的列中用零替换 NaN 值? - How to replace NaN values by Zeroes in a column of a Pandas Dataframe? 熊猫数据框NaN值替换为任何值 - Pandas Dataframe NaN values replace by no values Python pandas 用模式(同一列 -A)相对于 Pandas 数据帧中的另一列替换一列(A)的 NaN 值 - Python pandas replace NaN values of one column(A) by mode (of same column -A) with respect to another column in pandas dataframe Pandas Dataframe - 如果列值条件,将 NaN 替换为 0 - Pandas Dataframe - replace NaN with 0 if column value condition 用pandas中的数据框中的NaN替换所有值 - Replace all values with NaN in the dataframe in pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM