gpt4 book ai didi

python - 有条件 dropna() Pandas

转载 作者:行者123 更新时间:2023-12-01 08:49:12 26 4
gpt4 key购买 nike

我有一个相当简单的问题:我想根据条件从 DataFrame 中删除行。数据框看起来像这样:

Program        act
Original RO A
Original RO nan
Followup RO B
Followup RO nan
Integral RO nan

我只想删除Original ROIntegral RO 程序的空值。所以它应该看起来像这样:

Program        act
Original RO A
Followup RO B
Followup RO nan

当我尝试对数据帧的一部分进行某些操作并保持其余部分不变时,我总是遇到问题。

我尝试过这个:

df.loc[df.Program.str.match('^(Original|Integral)')] = df.dropna()

但是不起作用。我究竟做错了什么?提前致谢!

最佳答案

您可以使用 isin() 检查条件和 isna()并对数据框进行子集化。

lst = ['Original RO', 'Integral RO']

df = df[~(df['Program'].isin(lst) & df['act'].isna())]

# Another one using drop.
# df = df.drop(df[(df['Program'].isin(lst) & df['act'].isna())].index)

print (df)

Program act
0 Original RO A
2 Followup RO B
3 Followup RO NaN

关于python - 有条件 dropna() Pandas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53198369/

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