0
$\begingroup$

if i want to apply lambda with multiple condition how should I do it?

df.train.age.apply(lambda x:0 (if x>=0 and x<500))

or is there much better methods?

$\endgroup$
1
$\begingroup$

Alternatively, you can use loc:

import pandas as pd

df = pd.DataFrame({"age": [-100, 300, 400, 500, 600, 700]})
df["age"].loc[(df["age"] < 500) & (df["age"] >= 0)] = 0

Now your df looks like this:

    age
0   -100
1   0
2   0
3   500
4   600
5   700
| improve this answer | |
$\endgroup$
0
$\begingroup$

You can use Nested List comprehension within the lambda function.

Or

Write a function and call the function on your series using Lambda

| improve this answer | |
$\endgroup$
0
$\begingroup$

The most straightforward way is simply define a function.

Lambda function is great when the function is simple and straightforward otherwise it would be a pain to read and hence not really a recommended practice.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.