我有一个大数据框,当所有列都是 nan 或没有有限值时,我想拆分它。我正在寻找类似于帖子 Drop rows of pandas dataframe that don't have finite values in certain variable(s) 的内容但我不想放弃这些行。
我目前使用的是 pandas 0.16.0
请您参考如下方法:
通过调用 index.difference 从 dropna:
In [69]:
df = pd.DataFrame({'a':[0,np.NaN, 0], 'b':[np.NaN, np.NaN, 1]})
df = pd.concat([df]*10000, ignore_index=True)
%timeit df[df.apply(lambda x: x.isnull().all(), axis=1)]
%timeit df.loc[df.index.difference(df.dropna(how='all').index)]
1 loops, best of 3: 2.82 s per loop
100 loops, best of 3: 8.95 ms per loop
可以看到对于30k行的df,后一种方法要快很多
