gpt4 book ai didi

python - Pandas DataFrame 根据条件删除 NaN 行?

转载 作者:行者123 更新时间:2023-12-02 02:42:43 24 4
gpt4 key购买 nike

Pandas DataFrame 根据条件删除 NaN 行。

我正在尝试删除gender==malestatus == NaN 的行。

示例 df:

        name     status        gender   leaves
0 tom NaN male 5
1 tom True male 6
2 tom True male 7
3 mary True female 1
4 mary NaN female 10
5 mary True female 15
6 john NaN male 2
7 mark True male 3

预期输出:

        name     status        gender   leaves
0 tom True male 6
1 tom True male 7
2 mary True female 1
3 mary NaN female 10
4 mary True female 15
5 mark True male 3

最佳答案

您可以使用isna (或 isnull)函数来获取值为 NaN 的行。有了这些知识,您可以使用以下内容过滤数据框:

conditions = (df.gender == 'male')&(df.status.isna())
filtered_df = df[~conditions]

关于python - Pandas DataFrame 根据条件删除 NaN 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63300329/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com