Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.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 熊猫按列名聚合数据,并按步骤5对其中的数据求和_Python_Pandas_Numpy - Fatal编程技术网

Python 熊猫按列名聚合数据,并按步骤5对其中的数据求和

Python 熊猫按列名聚合数据,并按步骤5对其中的数据求和,python,pandas,numpy,Python,Pandas,Numpy,我有这样的数据: timestamp 101 100 105 109 110 112 2020-11-01 12:00:00 4 3 5 0 0 1 2020-11-01 12:01:00 4 9 5 3 1 1 2020-11-01 12:02:00 4

我有这样的数据:

timestamp                101     100     105    109    110      112
2020-11-01 12:00:00       4       3       5      0      0        1        
2020-11-01 12:01:00       4       9       5      3      1        1
2020-11-01 12:02:00       4       15      0      3      2        2 
2020-11-01 12:03:00       4       15      0      3      2        2
2020-11-01 12:05:00       4       15      0      3      2        3
2020-11-01 12:06:00       4       15      0      3      2        0
我希望它按列分组,步骤为5,并对内部数据求和 生成的数据帧是这样的:在进行聚合之前,应首先对列进行排序:

timestamp                100     105     110       
2020-11-01 12:00:00       12       0       1               
2020-11-01 12:01:00       18       4       2          
2020-11-01 12:02:00       19       5       4           
2020-11-01 12:03:00       19       5       4  
... 
...        
此外,要将缺少的行与前一行数据12:04:00一起添加,请尝试

out = df.set_index('timestamp').groupby(lambda x : int(x)//5*5,axis=1).sum()
Out[295]: 
                    100  105  110
timestamp                        
2020-11-0112:00:00    7    5    1
2020-11-0112:01:00   13    8    2
2020-11-0112:02:00   19    3    4
2020-11-0112:02:00   19    3    4
2020-11-0112:02:00   19    3    5
2020-11-0112:02:00   19    3    2
试一试

out = df.set_index('timestamp').groupby(lambda x : int(x)//5*5,axis=1).sum()
Out[295]: 
                    100  105  110
timestamp                        
2020-11-0112:00:00    7    5    1
2020-11-0112:01:00   13    8    2
2020-11-0112:02:00   19    3    4
2020-11-0112:02:00   19    3    4
2020-11-0112:02:00   19    3    5
2020-11-0112:02:00   19    3    2

上述方法非常有效。如果你能帮忙的话,我更新了问题的附加功能。我想用12:02:00的数据填充上面缺少的时间戳12:04:00,我的时间戳字段是索引,但它是时间戳格式的。你能帮忙吗?@PavneetSingh我想如果我们为你的后续新问题开辟一个新的话题会更好。上面的问题非常有效。如果你能帮忙的话,我更新了问题的附加功能。我想用12:02:00的数据填充上面缺少的时间戳12:04:00,我的时间戳字段是索引,但它是时间戳格式的。你能帮忙吗?@PavneetSingh我想如果我们为你的后续新问题开辟一个新的话题会更好