У меня есть набор данных, который содержит столбец с данными о старшинстве в компании формы: '9 years 9 months 14 days'
в формате str
. Я преобразовал их в float
for
цикла с регулярным выражением:
for row in range(len(df)):
target = df['seniority'][row]
content = re.findall(r'\d+', target)
content[0] = float(content[0])
content[1] = (float(content[1]))/12
content[2] = ((float(content[2]))/30)/12
content = sum(content)
df['seniority'][row] = content
Оно работает. Но мне интересен более эффективный и быстрый способ сделать это, если он существует.
Настройка:
df = pd.DataFrame(
{'sen': ['9 years 9 months 14 days', '2 years 4 months 12 days']
})
Вариант 1:
str.findall
списка с помощью str.findall
df['seniority'] = [
sum((float(x), float(y)/12, float(z)/365))
for x, y, z in df.sen.str.findall(r'(\d+)').values
]
# Result
sen seniority
0 9 years 9 months 14 days 9.788356
1 2 years 4 months 12 days 2.366210
Вариант 2:
str.extract
с div
и sum
:
df.sen.str.extract(r'.*?(\d+).*?(\d+).*?(\d+)').astype(float).div([1, 12, 365]).sum(1)
0 9.788356
1 2.366210
dtype: float64
Сроки:
df = pd.concat([df]*10000).reset_index(drop=True)
%%timeit
for row in range(len(df)):
target = df['sen'][row]
content = re.findall(r'\d+', target)
content[0] = float(content[0])
content[1] = (float(content[1]))/12
content[2] = ((float(content[2]))/30)/12
content = sum(content)
242 ms ± 1.67 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%%timeit
df['seniority'] = [
sum((float(x), float(y)/12, float(z)/365))
for x, y, z in df.sen.str.findall(r'(\d+)').values
]
29.9 ms ± 136 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
%timeit df.sen.str.extract(r'.*?(\d+).*?(\d+).*?(\d+)').astype(float).div([1,12, 365]).sum(1)
29 ms ± 143 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)