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

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

pandas changed column value condition of three other columns

问题

我有以下的pandas数据帧:

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

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

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

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

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

您可以如何做到这一点?

英文:

I have the following pandas dataframe:

df = pd.DataFrame({'pred': [1, 2, 3, 4],
                   'a': [0.4, 0.6, 0.35, 0.5],
                   'b': [0.2, 0.4, 0.32, 0.1],
                   'c': [0.1, 0, 0.2, 0.2],
                   '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:

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

How can I do this?

答案1

得分: 1

import pandas as pd

def row_cond(row):
    m_val = max(row[2:])
    if row[1] > m_val and m_val > 0.25:
        row[0] = 0
    return row

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

new_df = df.apply(row_cond, axis=1)

Output:

    pred    a       b       c       d
0   1.0     0.40    0.20    0.1     0.1
1   0.0     0.60    0.40    0.0     0.0
2   0.0     0.35    0.32    0.2     0.3
3   4.0     0.50    0.10    0.2     0.2
英文:
import pandas as pd

def row_cond(row):
    m_val = max(row[2:])
    if row[1]>m_val and m_val>0.25:
        row[0] = 0
    return row

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

new_df = df.apply(row_cond,axis=1)

Output:

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

答案2

得分: 1

创建一个布尔条件/掩码,然后使用 `loc` 将条件为 `True` 的值设置为 `0`

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


----------


       pred     a     b    c    d
    0     1  0.40  0.20  0.1  0.1
    1     0  0.60  0.40  0.0  0.0
    2     0  0.35  0.32  0.2  0.3
    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

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

   pred     a     b    c    d
0     1  0.40  0.20  0.1  0.1
1     0  0.60  0.40  0.0  0.0
2     0  0.35  0.32  0.2  0.3
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:

确定