我有一个如下所示的数据框
如何计算“列表”列中的平均值?
new = pd.DataFrame({
'list' : ['0 Minute 17 Seconds',
'0 Minute 50 Seconds',
'0 Minute 19 Seconds',
'0 Minute 29 Seconds']
})
答案 0 :(得分:3)
对于平均时间(以秒为单位),请使用Series.str.extract
强制转换为string
s:
df = new['list'].str.extract('(\d+)\s+Minute\s+(\d+)\s+Seconds')
df.columns = ['min','sec']
out = (df['min'].astype(int) * 60 + df['sec'].astype(int)).mean()
print (out)
28.75