gpt4 book ai didi

python - Pandas :在给定条件下填充nans

转载 作者:太空宇宙 更新时间:2023-11-03 11:40:57 24 4
gpt4 key购买 nike

我正在努力解决一些看似微不足道但显然并非如此的事情。一般图片:data - pandas dataframe - 包含(除其他外)TOTAL_VISITSNUM_PRINTS 列。

目标:给定 num_prints 参数,找到 NUM_prints = num_prints 的行,并用给定的数字填充 nan

我停下来的地方,它不再有意义了:

indices= data['NUM_PRINTS'] == num_prints

data.loc[indices,'TOTAL_VISITS'].fillna(5,inplace=True)

就我所知道和阅读的内容而言,这应该是可行的。在实践中没有用任何东西填充 nans,看起来它可以使用副本或其他东西,因为它没有改变原始对象中的任何东西。

什么有效:

data.loc[indices,'TOTAL_VISITS'] = 2

这确实在 num_print 条件下用 2 填充了列,但不考虑 nans。

data['TOTAL_VISITS'].fillna(0, inplace=True)

这确实用 0 填充了总访问量中的 nans,但没有考虑 num_prints 条件。

我对 .iloc 的常规 for 循环有点绝望,而且条件花费的时间太长,我无法处理。

最佳答案

我觉得这个不错

data['TOTAL_VISITS'] = np.where(data['NUM_PRINTS'] == 1, 100, data['TOTAL_VISITS'])

关于python - Pandas :在给定条件下填充nans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49477107/

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