pandas 熊猫数据框 fillna() 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39192614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:54:30  来源:igfitidea点击:

pandas dataframe fillna() not working?

pythonpandasdataframemissing-data

提问by user21478

I have a data set in which I am performing a principal components analysis (PCA). I get a ValueErrormessage when I try to transform the data. Below is some of the code:

我有一个数据集,我正在其中执行主成分分析 (PCA)。ValueError当我尝试转换数据时收到一条消息。下面是部分代码:

import pandas as pd
import numpy as np
import matplotlib as mpl
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA as sklearnPCA

data = pd.read_csv('test.csv',header=0)
X = data.ix[:,0:1000].values   # values of 1000 predictor variables
Y = data.ix[:,1000].values     # values of binary outcome variable
sklearn_pca = sklearnPCA(n_components=2)
X_std = StandardScaler().fit_transform(X)

It is here that I get the following error message:

在这里,我收到以下错误消息:

ValueError: Input contains NaN, infinity or a value too large for dtype('float64').

So I then checked whether the original data set had any NaN values:

所以我然后检查原始数据集是否有任何 NaN 值:

print(data.isnull().values.any())   # prints True
data.fillna(0)                      # replace NaN values with 0
print(data.isnull().values.any())   # prints True

I don't understand why data.isnull().values.any()is still printing Trueeven after I replaced the NaN values with 0.

我不明白为什么即使在我用 0 替换 NaN 值之后data.isnull().values.any()仍然打印True

回答by Jesse

There are two way to achieve, try replace in place:

有两种实现方式,尝试就地替换:

import pandas as pd

data = pd.DataFrame(data=[0,float('nan'),2,3])   
print('BEFORE:', data.isnull().values.any())   # prints True

# fillna function
data.fillna(0, inplace=True)

print('AFTER:',data.isnull().values.any())   # prints False now :)

Or, use returned object:

或者,使用返回的对象:

data = data.fillna(0)

Both case have same result as following:

两种情况的结果相同,如下所示:

BEFORE: True
AFTER: False

回答by Jean-Fran?ois Fabre

You have to replace data by the returned object from fillna

你必须用返回的对象替换数据 fillna

Small reproducer:

小型复制器:

import pandas as pd

data = pd.DataFrame(data=[0,float('nan'),2,3])

print(data.isnull().values.any())   # prints True
data = data.fillna(0)                      # replace NaN values with 0
print(data.isnull().values.any())   # prints False now :)