【发布时间】:2017-06-28 12:50:35
【问题描述】:
我有一个数据框,我希望能够使用 np.where 根据给定条件查找某些元素,然后使用 pd.drop 擦除与使用 np.where 找到的索引对应的元素。
即,
idx_to_drop = np.where(myDf['column10'].isnull() | myDf['column14'].isnull())
myDf.drop(idx_to_drop)
但我得到一个值错误,因为 drop 不采用 numpy 数组索引。有没有办法使用 np.where 和 pandas 中的一些 drop 函数来实现这一点?
【问题讨论】:
-
你不能这样做:
mask = myDf['column10'].isnull() | myDf['column14'].isnull()然后myDf[~mask]? -
@Divakar 非常感谢!我可以,我也在寻找某种使用 np.where 的方法,因为它在我可以执行的操作方面非常灵活。我个人也喜欢知道我正在擦除哪些索引。尽管如此,这更像是一个愿望,因为面具看起来很棒!