我的数据具有等于正无穷大和负无穷大的值。Vaex 具有 的功能dropna
,dropmissing
但dropnan
不能用于删除非有限值。
我目前的方法是遍历感兴趣的每一列并覆盖过滤数据集,从每一列中删除非有限值:
...
for col in cols:
df = df[df.col.isfinite()]
...
虽然这种方法确实给了我正确的结果,但它似乎效率很低,因为它需要很长时间才能运行,即使我的数据集只有几行和几千列。
在 Vaex 中删除具有非有限值的行的首选方法是什么?
更新:
这是一个工作示例,用于演示我在即使是微不足道的数据集上也遇到的缓慢:
import vaex
import numpy as np
import pandas as pd
#create a dummy data frame with 1000 columns and a few rows, some with nan/inf
arr= []
for i in range(1000):
arr.append([1] * 1 + [2] * 1 + [3] * 1 + [0] * 1 + [np.inf] * 1 + [-np.inf] * 1 + [np.nan] * 1)
df = pd.DataFrame(arr)
df = df.transpose()
df.columns = df.columns.map(str)
df = df.add_prefix('a')
df = vaex.from_pandas(df)
#eliminate rows that are not finite
for col in df.columns.keys(): #<-- this loop takes several minutes to run, I would expect it to be nearly instantaneous
df = df[df[col].isfinite()]
df
更新 2:单元格中的值略有不同,另一种选择有限记录的方法可以快速工作但返回不正确的结果:
import vaex
import numpy as np
import pandas as pd
arr= []
for i in range(2):
if i == 1:
arr.append([np.inf] * 1 + [2] * 1 + [3] * 1 + [0] * 1 + [1] * 1 + [1] * 1 + [1] * 1)
else:
arr.append([1] * 1 + [2] * 1 + [3] * 1 + [0] * 1 + [np.inf] * 1 + [-np.inf] * 1 + [np.nan] * 1)
df = pd.DataFrame(arr)
df = df.transpose()
df.columns = df.columns.map(str)
df = df.add_prefix('a')
df = vaex.from_pandas(df)
df
# a0 a1
0 1 inf
1 2 2
2 3 3
3 0 0
4 inf 1
5 -inf 1
6 nan 1
is_col_finite = np.array([df[col].isfinite() for col in df.columns.keys()])
all_finite = np.all(is_col_finite, axis=0)
df = df[all_finite]
df
# a0 a1
0 2 2
1 3 3
2 0 0
3 inf 1
4 -inf 1
5 nan 1