我正在尝试将df中的24小时时间内的列替换为总秒数。下面的代码生成随机df。
downloadFile() {
var app = this;
var url = baseRoute + 'files/' + this.fileId + '/download';
axios.get(url, {
headers: {
Authorization: 'bearer ' + this.Authentication.tokenData().token,
responseType : "blob"
}
}).then(function (response) {
var today = ((new Date()).toISOString()).slice(2,10),
filename = today.replace(/-/gi, "")+' '+ app.fileTitle;
let blob = new Blob([response.data], {
type: response.headers['content-type']
});
let link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = filename;
link.click();
}).catch(function (response) {
console.log(response);
});
}
输出:
import pandas as pd
import random
def randomTime():
rtime = int(random.random()*86400)
hours = int(rtime/3600)
minutes = int((rtime - hours*3600)/60)
seconds = rtime - hours*3600 - minutes*60
time_string = '%02d:%02d:%02d' % (hours, minutes, seconds)
return time_string
time = [randomTime() for _ in range(8)]
k = 5
N = 8
d = ({'Time' : (time),
'Events' : ['ABC','DEF','GHI','JKL','ABC','DEF','GHI','JKL'],
'Number1' : ['xx','xx',1,'xx','xx','xx',2,'xx'],
'Number2' : ['xx',1,'xx',1,'xx',2,'xx',2]})
df = pd.DataFrame(data=d)
我可以将Time转换为总秒数并使用以下内容返回单独的系列:
Events Number1 Number2 Time
0 ABC xx xx 07:02:30
1 DEF xx 1 03:34:02
2 GHI 1 xx 01:38:11
3 JKL xx 1 14:50:43
4 ABC xx xx 06:05:53
5 DEF xx 2 18:50:44
6 GHI 2 xx 17:09:42
7 JKL xx 2 21:09:28
输出:
Time_secs = pd.to_timedelta(df['Time']).dt.total_seconds()
但我想将数据框中的Time列替换为总秒数。不要导出一个单独的系列,我必须将它连接回df。
所以我希望能够提供以下内容:
0 25350.0
1 12842.0
2 5891.0
3 53443.0
4 21953.0
5 67844.0
6 61782.0
7 76168.0
答案 0 :(得分:2)
为什么不直接将结果分配给列?
df['Time'] = pd.to_timedelta(df['Time']).dt.total_seconds()