pandas如何通过一行生成多行

use*_*333 5 python pandas

时间戳速度

 1. 2014-12-04 12:30:10  104,105,105,106,106,106,99,90
 2. 2014-12-04 12:32:19  86,86,87,88,88,89,90,92,93,95,97,100,102,104,1...
 3. 2014-12-04 12:32:58  110,110,110,110,110,110,110,110,110,110,110,10..
Run Code Online (Sandbox Code Playgroud)

DatetimeIndex: 24 个条目,2014-12-04 12:30:10 到 2014-12-04 12:29:13 数据列(共 1 列):速度 24 个非空对象

我想像这样传输 DataFrame:

timestamp                                              speeds               

 1. 2014-12-04 12:30:10                                   104
 2. 2014-12-04 12:30:11                                   105
 3. 2014-12-04 12:30:12                                   105
 4. ....
 5. 2014-12-04 12:32:17                                   90
 6. 2014-12-04 12:32:18                    88 (resample and fill the timestamp and the mean speed value)
 7. 2014-12-04 12:32:19                                   86
 8. 2014-12-04 12:32:20                                   86
 9. 2014-12-04 12:32:21                                   87
Run Code Online (Sandbox Code Playgroud)

有简单的功能可以做到这一点吗?或者只逐行迭代并解析字段?

Mar*_*cin 0

不确定 pandas,但你可以用纯 python 来完成。很难,我不知道你所说的“(重新采样并填充时间戳和平均速度值)”是什么意思。但如果没有这个,你可以如下:

from datetime import datetime, timedelta

in_s = ["2014-12-04 12:30:10  104,105,105,106,106,106,99,90",
        "2014-12-04 12:32:19  86,86,87,88,88,89,90,92,93,95,97,100,102,104",
        "2014-12-04 12:32:58  110,110,110,110,110,110,110,110,110,110,110"]

for row in in_s:
    date_str,time_str, entries_str = row.split()
    #print(a_date,a_time, entries)
    a_time = datetime.strptime(time_str, "%H:%M:%S")
    for e in entries_str.split(','):      
        print(date_str, datetime.strftime(a_time, "%H:%M:%S"), e)
        a_time = a_time + timedelta(seconds=1)
Run Code Online (Sandbox Code Playgroud)

这导致:

2014-12-04 12:30:10 104
2014-12-04 12:30:11 105
2014-12-04 12:30:12 105
2014-12-04 12:30:13 106
2014-12-04 12:30:14 106
2014-12-04 12:30:15 106
2014-12-04 12:30:16 99
2014-12-04 12:30:17 90
2014-12-04 12:32:19 86
2014-12-04 12:32:20 86
2014-12-04 12:32:21 87
2014-12-04 12:32:22 88
2014-12-04 12:32:23 88
2014-12-04 12:32:24 89
2014-12-04 12:32:25 90
2014-12-04 12:32:26 92
2014-12-04 12:32:27 93
2014-12-04 12:32:28 95
2014-12-04 12:32:29 97
2014-12-04 12:32:30 100
2014-12-04 12:32:31 102
2014-12-04 12:32:32 104
2014-12-04 12:32:58 110
2014-12-04 12:32:59 110
2014-12-04 12:33:00 110
2014-12-04 12:33:01 110
2014-12-04 12:33:02 110
2014-12-04 12:33:03 110
2014-12-04 12:33:04 110
2014-12-04 12:33:05 110
2014-12-04 12:33:06 110
2014-12-04 12:33:07 110
2014-12-04 12:33:08 110
Run Code Online (Sandbox Code Playgroud)