The most straigt forward way is to use pandas.DataFrame.apply(...,axis=1)
to apply a function across your rows of the tables.
import numpy as np
import pandas as pd
df = pd.DataFrame({'msr':['1.82±0.10','1.72±.8','1.93±.7']})
def fun(row):
v,e = row['msr'].split('±')
row['val'] = float(v)
row['err'] = float(e)
return row
df = df.apply(fun,axis=1)
#adding a new systematic uncertainty in quadrature
syst_err = 0.05
df['tot_err'] = np.sqrt(df['err']**2 + syst_err**2)
Efficiency-wise it is (always) better to not specify a function yourself but use in-built functions. Such as pandas.DataFrame.str.split()
.
val, err = np.array(df['msr'].str.split('±').to_list(),dtype=float).transpose()
df['val'] = val
df['err'] = err
A quick run-time comparison gives 2.75 ms ± 134 μs
for the first approach and 425 μs ± 5.35 μs
for the second approach.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…