Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 重新组合数据帧_Python_Pandas - Fatal编程技术网

Python 重新组合数据帧

Python 重新组合数据帧,python,pandas,Python,Pandas,大宗报价 我有以下数据框: uniquie_id gsett start end catpri rep 0 000001_01 2 1900-01-01 04:00:00 1900-01-01 07:50:00 1 23 1 000001_01 2 1900-01-01 07:50:00 1900-01-01 08:00:00 2 1 2

大宗报价

我有以下数据框:

        uniquie_id gsett               start                 end  catpri  rep
0        000001_01     2 1900-01-01 04:00:00 1900-01-01 07:50:00       1   23
1        000001_01     2 1900-01-01 07:50:00 1900-01-01 08:00:00       2    1
2        000001_01     2 1900-01-01 08:00:00 1900-01-01 08:10:00       3    1
3        000001_01     2 1900-01-01 08:10:00 1900-01-01 08:30:00       4    2
4        000001_01     2 1900-01-01 08:30:00 1900-01-01 09:50:00       5    8
5        000001_01     2 1900-01-01 09:50:00 1900-01-01 10:00:00       2    1
6        000001_01     2 1900-01-01 10:00:00 1900-01-01 11:50:00       6   11
7        000001_01     2 1900-01-01 11:50:00 1900-01-01 12:00:00       4    1
8        000001_01     2 1900-01-01 12:00:00 1900-01-01 13:50:00       4   11
9        000001_01     2 1900-01-01 13:50:00 1900-01-01 14:50:00       7    6
10       000001_01     2 1900-01-01 14:50:00 1900-01-01 15:50:00       8    6
11       000001_01     2 1900-01-01 15:50:00 1900-01-01 16:00:00       3    1
12       000001_01     2 1900-01-01 16:00:00 1900-01-01 16:50:00       3    5
13       000001_01     2 1900-01-01 16:50:00 1900-01-01 17:50:00       3    6
14       000001_01     2 1900-01-01 17:50:00 1900-01-01 18:00:00       9    1
15       000001_01     2 1900-01-01 18:00:00 1900-01-01 18:40:00       5    4
16       000001_01     2 1900-01-01 18:40:00 1900-01-01 18:50:00       9    1
17       000001_01     2 1900-01-01 18:50:00 1900-01-01 20:50:00       4   12
18       000001_01     2 1900-01-01 20:50:00 1900-01-01 21:20:00       7    3
19       000001_01     2 1900-01-01 21:20:00 1900-01-01 21:30:00       8    1
20       000001_01     2 1900-01-01 21:30:00 1900-01-01 21:50:00       8    2
21       000001_01     2 1900-01-01 21:50:00 1900-01-01 22:00:00      10    1
22       000001_01     2 1900-01-01 22:00:00 1900-01-02 00:50:00      10   17
23       000001_01     2 1900-01-01 00:50:00 1900-01-02 04:00:00      13   19
我需要制作一个数据框,每行中有唯一的_id,因为列的日期从1900-01-01 04:00到1900-01-02 04:00,间隔10分钟。要放入每行/每列的值是catpri值

应该是这样的:

          1900-01-01 04:00:00 1900-01-01 04:10:00  ... 1900-01-02 03:50:00 1900-01-02 04:00:00
000001_01                   1                   1  ...                  13                  13
我曾尝试编写一个函数并使用apply,但仍然需要很长时间


有什么建议可以快速做到这一点吗?

在我的评论中,我认为这是df.pivot的一个用例,如下所述:

df.pivot(index='unique_id',columns='start', values='catpri')

start   1900-01-01 00:50:00 1900-01-01 04:00:00 1900-01-01 07:50:00 1900-01-01 08:00:00 1900-01-01 08:10:00 1900-01-01 08:30:00 1900-01-01 09:50:00 1900-01-01 10:00:00 1900-01-01 11:50:00 1900-01-01 12:00:00 ... 1900-01-01 16:50:00 1900-01-01 17:50:00 1900-01-01 18:00:00 1900-01-01 18:40:00 1900-01-01 18:50:00 1900-01-01 20:50:00 1900-01-01 21:20:00 1900-01-01 21:30:00 1900-01-01 21:50:00 1900-01-01 22:00:00
unique_id                                                                                   
000001_01   13      1       2       3       4       5       2       6       4       4   ...     3       9       5       9       4       7       8       8       10      10
包括通过@FabienP重新采样以获得缺失的间隔:

df.set_index('start').resample(rule='10T').ffill().reset_index().pivot(index='unique_id',columns='start', values='catpri')

start   1900-01-01 00:50:00 1900-01-01 01:00:00 1900-01-01 01:10:00 1900-01-01 01:20:00 1900-01-01 01:30:00 1900-01-01 01:40:00 1900-01-01 01:50:00 1900-01-01 02:00:00 1900-01-01 02:10:00 1900-01-01 02:20:00 ... 1900-01-01 20:30:00 1900-01-01 20:40:00 1900-01-01 20:50:00 1900-01-01 21:00:00 1900-01-01 21:10:00 1900-01-01 21:20:00 1900-01-01 21:30:00 1900-01-01 21:40:00 1900-01-01 21:50:00 1900-01-01 22:00:00
unique_id                                                                                   
000001_01   13  13  13  13  13  13  13  13  13  13  ... 4   4   7   7   7   8   8   8   10  10
1 rows × 128 columns

你的预期产出与你所说的不符。你能澄清一下吗?你只是想转置你的数据帧吗?为了回显@AvyWam,这看起来像一个转置或pivot/pivot_表,使用pandasNo中的内置函数可以显著加快速度,我不想转置。这是一种将我的原始数据帧转换为每一行的catpri值,以便在该时间间隔内由唯一的_id完成。最后我想要的是每个唯一的_id对应一行,144列从1900-01-01 04:00到1900-01-02 04:00:00。可能的重复可以尝试在10分钟的间隔内获取catpri。你说得对,我甚至没有注意到时间序列中缺少的行。