I want to create new column based on existing old column using multiple conditions how to do that
Question:
imagine i have a column days
days
190
567
55
I want to create a new column based on the condition that
df[‘new_colum’] =
if day < 180:
print(y)
elif( days >180 & < 365):
print(d)
else:
print(h)
how do i do this in python and any alternative for if condition
Answers:
You can use df.apply
:
df['new_col'] = df.days.apply(lambda x: y if x < 180 else (d if 180 < x < 365 else h))
imagine i have a column days
days |
---|
190 |
567 |
55 |
I want to create a new column based on the condition that
df[‘new_colum’] =
if day < 180:
print(y)
elif( days >180 & < 365):
print(d)
else:
print(h)
how do i do this in python and any alternative for if condition
You can use df.apply
:
df['new_col'] = df.days.apply(lambda x: y if x < 180 else (d if 180 < x < 365 else h))