pandas修改了三个其他列的条件值

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

pandas changed column value condition of three other columns

问题

我有以下的pandas数据帧:

  1. df = pd.DataFrame({'pred': [1, 2, 3, 4],
  2. 'a': [0.4, 0.6, 0.35, 0.5],
  3. 'b': [0.2, 0.4, 0.32, 0.1],
  4. 'c': [0.1, 0, 0.2, 0.2],
  5. 'd': [0.3, 0, 0.1, 0.2]})

我想根据列a、b、c、d的值更改'pred'列中的值,规则如下:

如果 列a的值大于列b、c、d的值之一
并且
如果 列b、c或d中的某一列的值大于0.25

那么将'pred'中的值更改为0。结果应如下所示:

  1. pred a b c d
  2. 0 1 0.4 0.20 0.1 0.1
  3. 1 0 0.6 0.40 0.0 0.0
  4. 2 0 0.35 0.32 0.2 0.3
  5. 3 4 0.5 0.10 0.2 0.2

您可以如何做到这一点?

英文:

I have the following pandas dataframe:

  1. df = pd.DataFrame({'pred': [1, 2, 3, 4],
  2. 'a': [0.4, 0.6, 0.35, 0.5],
  3. 'b': [0.2, 0.4, 0.32, 0.1],
  4. 'c': [0.1, 0, 0.2, 0.2],
  5. 'd': [0.3, 0, 0.1, 0.2]})

I want to change values on 'pred' column, based on columns a,b,c,d , as following:

if a has the value at column a is larger than the values of column b,c,d
and
if one of columns - b , c or d has value larger than 0.25

then change value in 'pred' to 0. so the results should be:

  1. pred a b c d
  2. 0 1 0.4 0.2 0.1 0.1
  3. 1 0 0.6 0.4 0.0 0.0
  4. 2 0 0.35 0.32 0.2 0.3
  5. 3 4 0.5 0.1 0.2 0.2

How can I do this?

答案1

得分: 1

  1. import pandas as pd
  2. def row_cond(row):
  3. m_val = max(row[2:])
  4. if row[1] > m_val and m_val > 0.25:
  5. row[0] = 0
  6. return row
  7. df = pd.DataFrame({'pred': [1, 2, 3, 4],
  8. 'a': [0.4, 0.6, 0.35, 0.5],
  9. 'b': [0.2, 0.4, 0.32, 0.1],
  10. 'c': [0.1, 0, 0.2, 0.2],
  11. 'd': [0.1, 0, 0.3, 0.2]})
  12. new_df = df.apply(row_cond, axis=1)

Output:

  1. pred a b c d
  2. 0 1.0 0.40 0.20 0.1 0.1
  3. 1 0.0 0.60 0.40 0.0 0.0
  4. 2 0.0 0.35 0.32 0.2 0.3
  5. 3 4.0 0.50 0.10 0.2 0.2
英文:
  1. import pandas as pd
  2. def row_cond(row):
  3. m_val = max(row[2:])
  4. if row[1]>m_val and m_val>0.25:
  5. row[0] = 0
  6. return row
  7. df = pd.DataFrame({'pred': [1, 2, 3, 4],
  8. 'a': [0.4, 0.6, 0.35, 0.5],
  9. 'b': [0.2, 0.4, 0.32, 0.1],
  10. 'c': [0.1, 0, 0.2, 0.2],
  11. 'd': [0.1, 0, 0.3, 0.2]})
  12. new_df = df.apply(row_cond,axis=1)

Output:

  1. pred a b c d
  2. 0 1.0 0.40 0.20 0.1 0.1
  3. 1 0.0 0.60 0.40 0.0 0.0
  4. 2 0.0 0.35 0.32 0.2 0.3
  5. 3 4.0 0.50 0.10 0.2 0.2

答案2

得分: 1

  1. 创建一个布尔条件/掩码,然后使用 `loc` 将条件为 `True` 的值设置为 `0`
  2. cols = ['b', 'c', 'd']
  3. mask = df[cols].lt(df['a'], axis=0).all(1) & df[cols].gt(.25).any(1)
  4. df.loc[mask, 'pred'] = 0
  5. ----------
  6. pred a b c d
  7. 0 1 0.40 0.20 0.1 0.1
  8. 1 0 0.60 0.40 0.0 0.0
  9. 2 0 0.35 0.32 0.2 0.3
  10. 3 4 0.50 0.10 0.2 0.2
英文:

Create a boolean condition/mask then use loc to set value to 0 where condition is True

  1. cols = ['b', 'c', 'd']
  2. mask = df[cols].lt(df['a'], axis=0).all(1) & df[cols].gt(.25).any(1)
  3. df.loc[mask, 'pred'] = 0

  1. pred a b c d
  2. 0 1 0.40 0.20 0.1 0.1
  3. 1 0 0.60 0.40 0.0 0.0
  4. 2 0 0.35 0.32 0.2 0.3
  5. 3 4 0.50 0.10 0.2 0.2

huangapple
  • 本文由 发表于 2023年2月19日 22:05:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500672.html
匿名

发表评论

匿名网友

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

确定