简体   繁体   English

根据列值的条件应用条件 function

[英]Apply conditional function based on condition of column value

I have a data-frame where I would like to apply a simple function to every column except the first one.我有一个数据框,我想将一个简单的 function 应用于除第一列之外的每一列。 Take below as an example - although in reality my dataframe comprises hundreds of columns:以下面为例-尽管实际上我的 dataframe 包含数百列:

vals = [(0, 12, 0),
     (33, 0, 11),
     (44, 16, 21),
     (0, 32, 1),
     (66, 33, 27),
     (77, 0, 0)
     ]

df = pd.DataFrame(vals, columns=list('ABC'))

I would like to find a way I can instigate a rule whereby each value greater than 0 is replaced with a 1. Crucially, I do not want to apply this rule to the first column, which should remain as it is.我想找到一种方法,我可以制定一个规则,将每个大于 0 的值替换为 1。至关重要的是,我不想将此规则应用于应该保持原样的第一列。

The closest I have got is a lambda function, which isn't working at all:我得到的最接近的是 lambda function,它根本不工作:

df = df.apply(lambda x: 1 if x > 0 else 0 if x.name != 'A' else x)

When using the apply method, the applied function will either receive all the entire column (by default and if axis=0 ), or it will receive the entire row ( axis=1 ).使用apply方法时,应用的 function 将接收所有整列(默认情况下,如果axis=0 ),或者它将接收整行( axis=1 )。 In your case, the lambda function is an element-wise function, this is why you need to use the applymap method.在您的情况下,lambda function 是逐元素 function,这就是您需要使用applymap方法的原因。

df[['B', 'C']] = df[['B', 'C']].applymap(lambda x: 1 if x > 0 else 0)

You can do it this way:你可以这样做:

>>> cols = ['B','C']
>>> df[cols] = df[cols].gt(0).astype(int)
>>> df
    A  B  C
0   0  1  0
1  33  0  1
2  44  1  1
3   0  1  1
4  66  1  1
5  77  0  0

You can change cols for something like df.columns[1:] for a more general case.对于更一般的情况,您可以将cols更改为df.columns[1:]类的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM