Вы можете создать условия, которые, если недели, то взять значение и разделить на 52:
df = pd.DataFrame({'AgeInYears':['1 year', '1 year', '2 years', '3 weeks', '2 years',
'1 month', '3 weeks', '3 weeks']})
# split the column into 2
df = df['AgeInYears'].str.split(expand=True)
df[0] = df[0].astype(int)
0 1
0 1 year
1 1 year
2 2 years
3 3 weeks
4 2 years
5 1 month
6 3 weeks
7 3 weeks
тогда вы можете использовать np.select
для ваших условий:
conditions = [
(df[1].str.contains('year')),
(df[1].str.contains('week')),
(df[1].str.contains('month'))
]
choices = [
df[0],
df[0]/52,
df[0]/12
]
df['newValue'] = np.select(conditions,choices,default=np.nan)
0 1 newValue
0 1 year 1.000000
1 1 year 1.000000
2 2 years 2.000000
3 3 weeks 0.057692
4 2 years 2.000000
5 1 month 0.083333
6 3 weeks 0.057692
7 3 weeks 0.057692