Please tell me how to use lambda.

Asked 2 years ago, Updated 2 years ago, 111 views

It works well below, but

df['flag']=df['current'].apply(lambdax:1 if x>=5000 else0)

If you change it to the following, you will get an error.

df['flag']=df['current'].apply(lambdax:1 if x>=df['current2'] else0)

Error Message

ValueError: The truth value of a series is amazing. Use a.empty, a.bool(), a.item(), a.any() or a.all().

python pandas lambda

2022-09-30 18:27

1 Answers

If you want to use apply in more than one column, you must apply it to DataFrame instead of Series.

df['flag'] = df.apply (lambdax:1 if x.current>=x.current2else0,axis=1)

But let's use Pandas' vector operation for calculations like the question

df['flag']=(df['current']>=df['current2']).astype(int)


2022-09-30 18:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.