I knew vectorization faster thand apply in pandas.. but’ below benchmarck says apply is faster than vectorization.. I confused about that, Am i using vectorization wrongly?
# making data
data = [' example text '] * 1000000
df = pd.DataFrame(data, columns=['tr_content'])
# benchmark vectorization
start_time = time.time()
df['tr_content'] = df['tr_content'].str.strip()
df['tr_content'] = df['tr_content'].str[0].str.upper() + df['tr_content'].str[1:]
print("Method 1 execution time: ", time.time() - start_time)
# Recreate data
df = pd.DataFrame(data, columns=['tr_content'])
# benchmark apply
start_time = time.time()
capitalize_at_first = lambda x: (y := str(x).strip()) and y[0].upper() + y[1:]
df['tr_content'] = df['tr_content'].apply(capitalize_at_first)
print("Method 2 execution time: ", time.time() - start_time)