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?
Data Science Stack Exchange is a question and answer site for Data science professionals, Machine Learning specialists, and those interested in learning more about the field. It only takes a minute to sign up.
Sign up to join this communityAlternatively, 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
You can use Nested List comprehension within the lambda function.
Or
Write a function and call the function on your series using Lambda
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.