I have a data frame as shown below
Date in_days
2020-02-01 1
2020-02-06 6
2020-02-09 9
2020-02-03 3
2020-02-11 11
2020-02-21 21
2020-02-13 13
2020-02-29 29
2020-02-26 26
I would like to create a function which is will create a new column called t_factor from in_days as shown below.
t = in_days
if 0 < in_days <= 4:
t_factor = (3*in_days) + 2
else if 4 < in_days <= 12:
t_factor = 14
else:
t_factor = (in_days)**2 + (2*in_days) + 2
Expected output:
Date in_days t_factor
2020-02-01 1 5
2020-02-06 6 14
2020-02-09 9 14
2020-02-03 3 11
2020-02-11 11 14
2020-02-20 20 442
2020-02-13 10 12
2020-02-29 25 677
2020-02-26 2 8
t
in your code is the same with in_days
. In which case, you can do:
df['t_factor'] = np.select( (df['in_days'].gt(0) & df['in_days'].le(4),
df['in_days'].gt(4) & df['in_days'].le(12)),
(df['in_days']*3+2, 14), # is this 12 or 14?
df['in_days']**2 + df['in_days']*2 + 2)
Output:
Date in_days t_factor
0 2020-02-01 1 5
1 2020-02-06 6 14
2 2020-02-09 9 14
3 2020-02-03 3 11
4 2020-02-11 11 14
5 2020-02-20 20 442
6 2020-02-13 10 14
7 2020-02-29 25 677
8 2020-02-26 2 8
You can do it using map
function as follow:
df['A'].map(multiply)
where multiply
is the function name to apply.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.