I have a data-frame where I would like to apply a simple function to every column except the first one. Take below as an example - although in reality my dataframe comprises hundreds of columns:
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.
The closest I have got is a lambda function, which isn't working at all:
df = df.apply(lambda x: 1 if x > 0 else 0 if x.name != 'A' else x)
question from:
https://stackoverflow.com/questions/66065864/apply-conditional-function-based-on-condition-of-column-value 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…