Python Pandas:如何删除nan和-inf值

我有以下数据框

           time       X    Y  X_t0     X_tp0  X_t1     X_tp1  X_t2     X_tp2

0 0.002876 0 10 0 NaN NaN NaN NaN NaN

1 0.002986 0 10 0 NaN 0 NaN NaN NaN

2 0.037367 1 10 1 1.000000 0 NaN 0 NaN

3 0.037374 2 10 2 0.500000 1 1.000000 0 NaN

4 0.037389 3 10 3 0.333333 2 0.500000 1 1.000000

5 0.037393 4 10 4 0.250000 3 0.333333 2 0.500000

....

1030308 9.962213 256 268 256 0.000000 256 0.003906 255 0.003922

1030309 10.041799 0 268 0 -inf 256 0.000000 256 0.003906

1030310 10.118960 0 268 0 NaN 0 -inf 256 0.000000

我尝试了以下

df.dropna(inplace=True)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.40)

X_train = X_train.drop('time', axis=1)

X_train = X_train.drop('X_t1', axis=1)

X_train = X_train.drop('X_t2', axis=1)

X_test = X_test.drop('time', axis=1)

X_test = X_test.drop('X_t1', axis=1)

X_test = X_test.drop('X_t2', axis=1)

X_test.fillna(X_test.mean(), inplace=True)

X_train.fillna(X_train.mean(), inplace=True)

y_train.fillna(y_train.mean(), inplace=True)

但是,ValueError: Input contains NaN, infinity or a value too large for

dtype('float32').每当我尝试拟合回归模型时 ,我仍然会收到此错误fit(X_train, y_train)

我们如何同时删除NaN-inf值?

回答:

使用pd.DataFrame.isin并检查带有的行pd.DataFrame.any。最后,使用布尔数组对数据帧进行切片。

df[~df.isin([np.nan, np.inf, -np.inf]).any(1)]

time X Y X_t0 X_tp0 X_t1 X_tp1 X_t2 X_tp2

4 0.037389 3 10 3 0.333333 2.0 0.500000 1.0 1.000000

5 0.037393 4 10 4 0.250000 3.0 0.333333 2.0 0.500000

1030308 9.962213 256 268 256 0.000000 256.0 0.003906 255.0 0.003922

以上是 Python Pandas:如何删除nan和-inf值 的全部内容, 来源链接: utcz.com/qa/397711.html

回到顶部