python的out模式_如何用python中的DataFrame列的模式替换NA值?
我對Python(和本網站)完全陌生,目前正試圖用它們的模式替換特定數據幀列中的NA值。我試過了各種不起作用的方法。請幫我看看我做錯了什么:如何用python中的DataFrame列的模式替換NA值?
注意:我正在使用的所有列都是float64類型。我所有的代碼都會運行,但是當我在列中檢查空數量df[cols_mode].isnull().sum()時,它仍然是一樣的。
方法1:
cols_mode = ['race', 'goal', 'date', 'go_out', 'career_c']
df[cols_mode].apply(lambda x: x.fillna(x.mode, inplace=True))
我試圖Imputer方法太多,但遇到了同樣的結果
方法2:
for column in df[['race', 'goal', 'date', 'go_out', 'career_c']]:
mode = df[column].mode()
df[column] = df[column].fillna(mode)
方法3:
df['race'].fillna(df.race.mode(), inplace=True)
df['goal'].fillna(df.goal.mode(), inplace=True)
df['date'].fillna(df.date.mode(), inplace=True)
df['go_out'].fillna(df.go_out.mode(), inplace=True)
df['career_c'].fillna(df.career_c.mode(), inplace=True)
方法4: 我的方法變得越來越手動過程,最后這一個工程:
df['race'].fillna(2.0, inplace=True)
df['goal'].fillna(1.0, inplace=True)
df['date'].fillna(6.0, inplace=True)
df['go_out'].fillna(2.0, inplace=True)
df['career_c'].fillna(2.0, inplace=True)
+0
你是否看了替代方法? http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.replace.html –
+0
“方法2”有什么問題? –
+0
@MaxU方法2正在返回一個錯誤。 'mode'返回一個Series,而不是一個單一的值。 –
總結
以上是生活随笔為你收集整理的python的out模式_如何用python中的DataFrame列的模式替换NA值?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: eslint 保存自动格式化_ESLin
- 下一篇: 深圳python指南_深圳Pythonl