Rob*_*usk 8 python optimization pandas dask
我试图应用过滤器来删除 NA 太多的列到我的 dask 数据框中:
df.dropna(axis=1, how='all', thresh=round(len(df) * .8))
Run Code Online (Sandbox Code Playgroud)
不幸的是,dask dropnaAPI似乎与 Pandas 的API 略有不同,并且既不接受 anaxis也不接受threshold. 解决它的一种部分方法是逐列迭代并删除那些常量(无论它们是否填充了 NA,因为我不介意去掉常量):
for col in df.columns:
if len(df[col].unique()) == 1:
new_df = df.drop(col, axis = 1)
Run Code Online (Sandbox Code Playgroud)
但这并不能让我应用阈值。我可以通过添加手动计算阈值:
elif sum(df[col].isnull().compute()) / len(df[col]) > 0.8:
new_df = df.drop(col, axis = 1)
Run Code Online (Sandbox Code Playgroud)
但我不确定打电话compute,len此时会是最佳选择,我很想知道是否有更好的方法来解决这个问题?
小智 5
现在 Dask 有axis,thresh和subsetargs 可能会有所帮助。之前的答案可以改写为:
df.dropna(subset=columns_to_inspect, thresh=threshold_to_drop_na, axis=1)
Run Code Online (Sandbox Code Playgroud)
你是对的,没有办法通过使用来做到这一点df.dropna()。
我建议使用这个方程
df.loc[:,df.isnull().sum()<THRESHOLD]