pandas dropna-存储丢弃的行 [英] Pandas dropna - store dropped rows
问题描述
我正在使用 pandas. DataFrame.dropna 方法删除包含NaN的行.该函数返回一个数据帧,该数据帧不包含被删除的行,如文档所示.
I am using the pandas.DataFrame.dropna method to drop rows that contain NaN. This function returns a dataframe that excludes the dropped rows, as shown in the documentation.
如何将删除的行的副本存储为单独的数据框?是:
How can I store a copy of the dropped rows as a separate dataframe? Is:
mydataframe[pd.isnull(['list', 'of', 'columns'])]
始终确保返回dropna删除的相同行,假设dropna是用subset=['list', 'of', 'columns']
调用的?
always guaranteed to return the same rows that dropna drops, assuming that dropna is called with subset=['list', 'of', 'columns']
?
推荐答案
You can do this by indexing the original DataFrame by using the unary ~
(invert) operator to give the inverse of the NA free DataFrame.
na_free = df.dropna()
only_na = df[~df.index.isin(na_free.index)]
另一种选择是使用 ufunc ~
.
Another option would be to use the ufunc implementation of ~
.
only_na = df[np.invert(df.index.isin(na_free.index))]
这篇关于 pandas dropna-存储丢弃的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!