gpt4 book ai didi

python - pandas如何通过一行生成多行

转载 作者:行者123 更新时间:2023-12-02 00:58:44 24 4
gpt4 key购买 nike

时间戳速度

 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..

日期时间索引: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

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

最佳答案

不确定重新采样(很难从你的例子中说出你想做什么)。 pandas 还可以实现其他功能(可能不是最优雅的方式):

>>> df2 = df.apply(lambda x: pd.Series(x['speeds']),axis=1)
>>> df2['timestamp'] = df['timestamp']
>>> df2
0 1 2 3 4 5 6 7 8 9 10 11 12 13 timestamp
0 104 105 105 106 106 106 99 90 NaN NaN NaN NaN NaN NaN 2014-12-04 12:30:10
1 6 86 87 88 88 89 90 92 93 95 97 100 102 104 2014-12-04 12:32:19
>>>
>>> df2 = df2.set_index('timestamp').stack().reset_index()
>>> df2['timestamp'] = df2.apply(lambda x: x['timestamp'] + timedelta(seconds=x['level_1']), axis=1)
>>> del df2['level_1']
>>> df2
timestamp 0
0 2014-12-04 12:30:10 104
1 2014-12-04 12:30:11 105
2 2014-12-04 12:30:12 105
3 2014-12-04 12:30:13 106
4 2014-12-04 12:30:14 106
5 2014-12-04 12:30:15 106
6 2014-12-04 12:30:16 99
7 2014-12-04 12:30:17 90
8 2014-12-04 12:32:19 6
9 2014-12-04 12:32:20 86
10 2014-12-04 12:32:21 87
11 2014-12-04 12:32:22 88
12 2014-12-04 12:32:23 88
13 2014-12-04 12:32:24 89
14 2014-12-04 12:32:25 90
15 2014-12-04 12:32:26 92
16 2014-12-04 12:32:27 93
17 2014-12-04 12:32:28 95
18 2014-12-04 12:32:29 97
19 2014-12-04 12:32:30 100
20 2014-12-04 12:32:31 102
21 2014-12-04 12:32:32 104

关于python - pandas如何通过一行生成多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27370731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com