簡體   English   中英

pandas DataFrame:將nan值替換為對應列的中值

[英]pandas DataFrame: replace nan values with median of corresponding column

我的 pd dataframe 中有一些 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

任何建議表示贊賞。 謝謝

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM