Python Pandas Fillna中位数不起作用(Python Pandas Fillna Median not working)

编程入门 行业动态 更新时间:2024-10-25 00:31:42
Python Pandas Fillna中位数不起作用(Python Pandas Fillna Median not working)

我试图在包含多列和多行的数据框中填充所有的nans。 我正在使用它来训练一个多变量ML模型,所以我想用中位数填充每列的nans。 只是为了测试中值函数,我这样做了:

training_df.loc[[0]] = np.nan # Sets first row to nan print(training_df.isnull().values.any()) # Prints true because we just inserted nans test = training_df.fillna(training_df.median()) # Fillna with median print(test.isnull().values.any()) # Check afterwards

但是当我这样做时什么都没有发生,最后一行的打印仍然返回True。 如果我尝试改为使用像这样的中值函数:

training_df.fillna(training_df.median(), inplace=True)

没有任何反应。 如果我这样做:

training_df = training_df.fillna(training_df.median(), inplace=True)

Training_df变成无。 我该如何解决这个问题?

I am trying to fill all the nans in a dataframe containing multiple columns and several rows. I am using this to train a multi variate ML-model so I want to fill the nans for each column with the median. Just to test the median function I did this:

training_df.loc[[0]] = np.nan # Sets first row to nan print(training_df.isnull().values.any()) # Prints true because we just inserted nans test = training_df.fillna(training_df.median()) # Fillna with median print(test.isnull().values.any()) # Check afterwards

But when I do this nothing happens, the print of the last row still returns True. If I try to change to use the median function like this instead:

training_df.fillna(training_df.median(), inplace=True)

Nothing happens as well. If I do this:

training_df = training_df.fillna(training_df.median(), inplace=True)

Training_df becomes none. How can I solve this?

最满意答案

正如@thesilkworm建议的那样,首先将你的系列转换为数字。 下面是一个简单的例子:

import pandas as pd, numpy as np df = pd.DataFrame([[np.nan, np.nan, np.nan], [5, 1, 2, 'hello'], [1, 4, 3, 4], [9, 8, 7, 6]], dtype=object) df = df.fillna(df.median()) # fails df[df.columns] = df[df.columns].apply(pd.to_numeric, errors='coerce') df = df.fillna(df.median()) # works

As @thesilkworm suggested, convert your series to numeric first. Below is a minimal example:

import pandas as pd, numpy as np df = pd.DataFrame([[np.nan, np.nan, np.nan], [5, 1, 2, 'hello'], [1, 4, 3, 4], [9, 8, 7, 6]], dtype=object) df = df.fillna(df.median()) # fails df[df.columns] = df[df.columns].apply(pd.to_numeric, errors='coerce') df = df.fillna(df.median()) # works

更多推荐

本文发布于:2023-08-06 06:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446112.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中位数   不起作用   Pandas   Python   Fillna

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!