how to make pandas row value to zero when row above values are zeros and below value not equal to zero using python pandas

huangapple go评论110阅读模式
英文:

how to make pandas row value to zero when row above values are zeros and below value not equal to zero using python pandas

问题

我有一个pandas数据框,其中包含以下代码部分:

  1. df = pd.DataFrame({
  2. 'rpm': [2.0, 4.5, 5.6, 6.0, 7.0, 6.0, 0.0, 0.0, 3.0, 5.0, 9.0, 8.9,9.3,0,0,0,6,7,8,9,13]
  3. })

在上述数值中,如果为零且下一个值不为零,则当前值应为零,如何实现这一点?到目前为止,我使用了以下代码:

  1. for i in range(1, len(df) - 1):
  2. if df['rpm'].iloc[i-1] == 0 and df['rpm'].iloc[i+1] != 0 :
  3. df['rpm'].iloc[i] = 0

使用这段代码,我没有得到预期的输出。

期望的输出是:

  1. df = pd.DataFrame({
  2. 'rpm': [2.0, 4.5, 5.6, 6.0, 7.0, 6.0, 0.0, 0.0, 0.0, 5.0, 9.0, 8.9,9.3,0.0,0.0,0.0,0.0,7,8,9,13]
  3. })

如果上述一组值为零,下一组值为非零,那么值3必须被替换为零,下一组中的6必须被替换为零,如何实现这一点?

英文:

I have pandas data frame
df = pd.DataFrame({
'rpm': [2.0, 4.5, 5.6, 6.0, 7.0, 6.0, 0.0, 0.0, 3.0, 5.0, 9.0, 8.9,9.3,0,0,0,6,7,8,9,13]
})

here the the above values are zero and below values not equal to zero means the current value has to be zero how to achieve this so far i used this code

  1. for i in range(1, len(df) - 1):
  2. if df['rpm'].iloc[i-1] == 0 and df['rpm'].iloc[i+1] != 0 :
  3. df['rpm'].iloc[i] = 0

print(df.to_string()) with this code i am not getting expected output
expected output is
df = pd.DataFrame({
'rpm': [2.0, 4.5, 5.6, 6.0, 7.0, 6.0, 0.0, 0.0, 0.0, 5.0, 9.0, 8.9,9.3,0.0,0.0,0.0,0.0,7,8,9,13]
})

if above set of values are zero below set of values are non zero means the value 3 has to be replaced by 0 and in the next set 6 has to replaced by 0 how to do this

答案1

得分: 2

你可以使用 shift 来检查条件:

  1. df.loc[df['rpm'].shift(1).eq(0) & df['rpm'].shift(-1).ne(0), 'rpm'] = 0
  2. print(df)
  3. # 输出:
  4. rpm
  5. 0 2.0
  6. 1 4.5
  7. 2 5.6
  8. 3 6.0
  9. 4 7.0
  10. 5 6.0
  11. 6 0.0
  12. 7 0.0
  13. 8 0.0 # 此处,旧值: 3
  14. 9 5.0
  15. 10 9.0
  16. 11 8.9
  17. 12 9.3
  18. 13 0.0
  19. 14 0.0
  20. 15 0.0
  21. 16 0.0 # 此处,旧值: 6
  22. 17 7.0
  23. 18 8.0
  24. 19 9.0
  25. 20 13.0

细节部分:

  1. m1 = df['rpm'].shift(1).eq(0)
  2. m2 = df['rpm'].shift(-1).ne(0)
  3. out = pd.concat([df['rpm'], m1, m2, m1&m2], keys=['rpm', 'm1', 'm2', 'all'], axis=1)
  4. print(out)
  5. # 输出
  6. rpm rpm rpm rpm
  7. 0 2.0 False True False
  8. 1 4.5 False True False
  9. 2 5.6 False True False
  10. 3 6.0 False True False
  11. 4 7.0 False True False
  12. 5 6.0 False False False
  13. 6 0.0 False False False
  14. 7 0.0 True True True # 此处,已经是 0
  15. 8 3.0 True True True # 此处,设置为 0
  16. 9 5.0 False True False
  17. 10 9.0 False True False
  18. 11 8.9 False True False
  19. 12 9.3 False False False
  20. 13 0.0 False False False
  21. 14 0.0 True False False
  22. 15 0.0 True True True # 此处,已经是 0
  23. 16 6.0 True True True # 此处,设置为 0
  24. 17 7.0 False True False
  25. 18 8.0 False True False
  26. 19 9.0 False True False
  27. 20 13.0 False True False
英文:

You can use shift to check the condition:

  1. df.loc[df['rpm'].shift(1).eq(0) & df['rpm'].shift(-1).ne(0), 'rpm'] = 0
  2. print(df)
  3. # Output:
  4. rpm
  5. 0 2.0
  6. 1 4.5
  7. 2 5.6
  8. 3 6.0
  9. 4 7.0
  10. 5 6.0
  11. 6 0.0
  12. 7 0.0
  13. 8 0.0 # HERE, old value: 3
  14. 9 5.0
  15. 10 9.0
  16. 11 8.9
  17. 12 9.3
  18. 13 0.0
  19. 14 0.0
  20. 15 0.0
  21. 16 0.0 # HERE, old value: 6
  22. 17 7.0
  23. 18 8.0
  24. 19 9.0
  25. 20 13.0

Details:

  1. m1 = df['rpm'].shift(1).eq(0)
  2. m2 = df['rpm'].shift(-1).ne(0)
  3. out = pd.concat([df['rpm'], m1, m2, m1&m2], keys=['rpm', 'm1', 'm2', 'all'], axis=1)
  4. print(out)
  5. # Output
  6. rpm rpm rpm rpm
  7. 0 2.0 False True False
  8. 1 4.5 False True False
  9. 2 5.6 False True False
  10. 3 6.0 False True False
  11. 4 7.0 False True False
  12. 5 6.0 False False False
  13. 6 0.0 False False False
  14. 7 0.0 True True True # HERE, already 0
  15. 8 3.0 True True True # HERE, set to 0
  16. 9 5.0 False True False
  17. 10 9.0 False True False
  18. 11 8.9 False True False
  19. 12 9.3 False False False
  20. 13 0.0 False False False
  21. 14 0.0 True False False
  22. 15 0.0 True True True # HERE, already 0
  23. 16 6.0 True True True # HERE, set to 0
  24. 17 7.0 False True False
  25. 18 8.0 False True False
  26. 19 9.0 False True False
  27. 20 13.0 False True False

huangapple
  • 本文由 发表于 2023年7月11日 13:52:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76659010.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定