如何检查行趋势并将失败案例的差异和差异百分比分别添加到单独的列中

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

How to check the row trend and add the difference and difference percentage in separate columns for the failing cases

问题

以下是翻译好的代码部分:

  1. import pandas as pd
  2. import numpy as np
  3. d = {'Cell': ['A', 'B', 'C', 'D', 'E'], 'D1': [5, 2, 2, 6, 6], 'D2': [np.nan, 5, 6, np.nan, 3], 'D3': [7, np.nan, 5, 5, np.nan], 'D6': [17, 3, np.nan, np.nan, 2]}
  4. df = pd.DataFrame(d)

希望对你有所帮助。如需进一步的翻译或解释,请随时提出。

英文:

An extension to the problem statement
https://stackoverflow.com/questions/75412399/how-to-check-each-row-trend-with-some-tolerance-by-ignoring-the-np-nan-values-in

  1. import pandas as pd
  2. import numpy as np
  3. d = {'Cell':['A','B','C','D','E'],'D1':[5, 2, 2, 6,6], 'D2':[np.nan, 5, 6, np.nan,3], 'D3':[7,np.nan, 5, 5,np.nan], 'D6':[17, 3, np.nan,np.nan,2]}
  4. df = pd.DataFrame(d)
  5. Cell D1 D2 D3 D6
  6. 0 A 5 NaN 7.0 17.0
  7. 1 B 2 5.0 NaN 3.0
  8. 2 C 2 6.0 5.0 NaN
  9. 3 D 6 NaN 5.0 NaN
  10. 4 E 6 3.0 NaN 2.0

i want output like this with additional columns diff and diff% along with is_increasing and failing columns

  1. Cell D1 D2 D3 D6 is_increasing? failing diff diff%
  2. 0 A 5 NaN 7.0 17.0 True NaN NaN NaN
  3. 1 B 2 5.0 NaN 3.0 False [D6] [-2] [40%]
  4. 2 C 2 6.0 5.0 NaN False [D3] [-1] [16.6%]
  5. 3 D 6 NaN 5.0 NaN False [D3] [-1] [16.6%]
  6. 4 E 6 3.0 NaN 2.0 False [D2, D6] [-3,-1] [50%,33%]

Explanation of the columns:

  1. is_increasing --> whether the values are strictly increasing or not
  2. failing --> columns whether strictly increasing is not followed when compared with previous value
  3. diff --> difference of the values where there is failing cases
  4. diff% --> difference in terms of percentages for the failing cases

between (6,5) numbers in the columns

  1. diff column --> 5-6=-1
  2. diff%--> 1-(5/6)=16.6%

Please let me the solution to this problem, i tried different ways but not able to come up with solution.

答案1

得分: 1

以下是您要翻译的内容:

  1. # 仅筛选相关列
  2. # 使用任何方法
  3. df2 = df.drop(columns='Cell')
  4. d = (df2.ffill(axis=1)
  5. .diff(axis=1)
  6. )
  7. m = (d.where(df2.notna())
  8. .lt(0)
  9. )
  10. df['is_increasing'] = ~m.any(axis=1)
  11. df['failing'] = (
  12. m.mul(df2.columns)
  13. .where(m).stack()
  14. .groupby(level=0).agg(list)
  15. )
  16. df['diff'] = (d
  17. .where(m).stack()
  18. .groupby(level=0).agg(list)
  19. )
  20. p = (df2.ffill(axis=1)
  21. .pct_change(axis=1)
  22. .mul(-100).round(2)
  23. )
  24. df['diff%'] = (p
  25. .where(m).stack()
  26. .groupby(level=0).agg(list)
  27. )
  28. print(df)

输出:

  1. Cell D1 D2 D3 D6 is_increasing failing diff diff%
  2. 0 A 5 NaN 7.0 17.0 True NaN NaN NaN
  3. 1 B 2 5.0 NaN 3.0 False [D6] [-2.0] [40.0]
  4. 2 C 2 6.0 5.0 NaN False [D3] [-1.0] [16.67]
  5. 3 D 6 NaN 5.0 NaN False [D3] [-1.0] [16.67]
  6. 4 E 6 3.0 NaN 2.0 False [D2, D6] [-3.0, -1.0] [50.0, 33.33]

请注意,代码中的注释部分未被翻译。

英文:

You can use:

  1. # filter only relevant columns
  2. # use any method
  3. df2 = df.drop(columns='Cell')
  4. d = (df2.ffill(axis=1)
  5. .diff(axis=1)
  6. )
  7. m = (d.where(df2.notna())
  8. .lt(0)
  9. )
  10. df['is_increasing'] = ~m.any(axis=1)
  11. df['failing'] = (
  12. m.mul(df2.columns)
  13. .where(m).stack()
  14. .groupby(level=0).agg(list)
  15. )
  16. df['diff'] = (d
  17. .where(m).stack()
  18. .groupby(level=0).agg(list)
  19. )
  20. p = (df2.ffill(axis=1)
  21. .pct_change(axis=1)
  22. .mul(-100).round(2)
  23. )
  24. df['diff%'] = (p
  25. .where(m).stack()
  26. .groupby(level=0).agg(list)
  27. )
  28. print(df)

Output:

  1. Cell D1 D2 D3 D6 is_increasing failing diff diff%
  2. 0 A 5 NaN 7.0 17.0 True NaN NaN NaN
  3. 1 B 2 5.0 NaN 3.0 False [D6] [-2.0] [40.0]
  4. 2 C 2 6.0 5.0 NaN False [D3] [-1.0] [16.67]
  5. 3 D 6 NaN 5.0 NaN False [D3] [-1.0] [16.67]
  6. 4 E 6 3.0 NaN 2.0 False [D2, D6] [-3.0, -1.0] [50.0, 33.33]

huangapple
  • 本文由 发表于 2023年2月14日 03:45:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440543.html
匿名

发表评论

匿名网友

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

确定