I am pretty new to pandas and have a tabular data which has 3 columns where (df.info()
gives object
type) values are both in s
and m:s
. I would like to convert all m:s
values to s
values. I'll leave some examples to clarify.
now:
VT FT TTPF
1:28.8 1:17.2 30.4
1:06.4 1:06.2 16.8
38.6 26.2 10.8
should be:
VT FT TTPF
88.8 77.2 30.4
66.4 66.2 16.8
38.6 26.2 10.8
I'm sorry if I couldn't provide all the details. Feel free to request edits
First select only strings columns by DataFrame.select_dtypes
and DataFrame.apply
custom function with Series.str.contains
for filter values with :
and then Series.str.split
with casting to floats, multiple by 60 and sum together in Series.mask
for rows with :
:
def f(x):
m = x.str.contains(':')
y = x[m].str.split(':', expand=True)
return x.mask(m, y[0].astype(float) * 60 + y[1].astype(float))
c = df.select_dtypes(object).columns
df[c] = df[c].apply(f).astype(float)
print (df)
VT FT TTPF
0 88.8 77.2 30.4
1 66.4 66.2 16.8
2 38.6 26.2 10.8
Another idea is use DataFrame.applymap
for elemetwise processing:
def f(x):
if ':' in str(x):
a, b = x.split(':')
return float(a) * 60 + float(b)
else:
return float(x)
df = df.applymap(f)
print (df)
VT FT TTPF
0 88.8 77.2 30.4
1 66.4 66.2 16.8
2 38.6 26.2 10.8