对子集使用dropna进行清理

2024-04-23 13:57:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要检查我的一个子集的完整性熊猫.DataFrame. 目前我正在做的是:

special = df[df.kind=='special']
others = df[df.kind!='special']

special = special.dropna(how='any')

all = pd.concat([special, others])

我想知道,我是否没有遗漏任何一个强大的Pandas API,使这成为可能?在


Tags: dataframepandasdfanyall子集howpd
1条回答
网友
1楼 · 发布于 2024-04-23 13:57:42

我无法从我正在编写的地方访问Pandas,但是^{}检查内容是否为null,^{}可以按行检查条件。在

因此,如果你这样做

(df.kind != 'special') | ~df.isnull().any(axis=1)

这将给出要保留的行。您可以对此表达式使用普通索引。 很有意思的是看看这是否会加快速度(它比解决方案检查更多的行,但可能会创建更小的数据帧)。在

相关问题 更多 >