熊猫分组滚动分位数

时间:2019-03-14 22:41:04

标签: python pandas pandas-groupby

我想做的是,在几年中每分钟取一个由<?php $file1 = fopen("Profiles/person1.player", "r"); $file2 = fopen("Profiles/person2.player", "r"); $text1 = fread($file1, filesize("Profiles/person1.player")); $text2 = fread($file2, filesize("Profiles/person2.player")); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Three.js</title> </head> <body> <style> body{ margin: 0%; overflow: hidden; } head{ margin: 0px; } </style> <script src="three.js"></script> <script src="prefabs.js"></script> <script> var seq1 = "<?php echo $text1; ?>"; var seq2 = "<?php echo $text2; ?>"; seq1.split(", "); seq2.split(", "); for (var i = 0; i < seq1.length; i++){ seq1[i] = parseFloat(seq1[i]); } for (var j = 0; j < seq2.length; j++){ seq2[j] = parseFloat(seq2[j]); } var sequence1 = seq1; var sequence2 = seq2; var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 0.1, 1000); var renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); camera.position.z = 0; camera.position.x = 15; camera.position.y = 7.5; scene.add(new THREE.AmbientLight(0xffffff, 0.5)); var directional = new THREE.DirectionalLight(0xffffff, 1); directional.position.set(3, 10, 3); scene.add(directional); var player1 = Player(0xbc3232); scene.add(player1.body); player1.body.position.set(0, 0, 5); var player2 = Player(0x4286f4); scene.add(player2.body); player2.body.position.set(0, 0, -5); var ground = new THREE.Mesh( new THREE.BoxGeometry(20, 1, 20), new THREE.MeshStandardMaterial({color: 0xdddddd}) ); ground.position.set(0, -2, 0); var boxingRing = { one: new THREE.Mesh( new THREE.CylinderGeometry(0.75, 0.75, 10, 16), new THREE.MeshStandardMaterial() ), two: new THREE.Mesh( new THREE.CylinderGeometry(0.75, 0.75, 10, 16), new THREE.MeshStandardMaterial() ), three: new THREE.Mesh( new THREE.CylinderGeometry(0.75, 0.75, 10, 16), new THREE.MeshStandardMaterial() ), four: new THREE.Mesh( new THREE.CylinderGeometry(0.75, 0.75, 10, 16), new THREE.MeshStandardMaterial() ), beams: { one: new THREE.Mesh( new THREE.TorusGeometry(13.5, 0.3, 16, 4), new THREE.MeshStandardMaterial({color: 0xbc3232}) ), two: new THREE.Mesh( new THREE.TorusGeometry(13.5, 0.3, 16, 4), new THREE.MeshStandardMaterial({color: 0x4286f4}) ), three: new THREE.Mesh( new THREE.TorusGeometry(13.5, 0.3, 16, 4), new THREE.MeshStandardMaterial({color: 0xdddddd}) ), } }; ground.add(boxingRing.one); boxingRing.one.position.set(-9.5, 0, -9.5); ground.add(boxingRing.two); boxingRing.two.position.set(9.5, 0, -9.5); ground.add(boxingRing.three); boxingRing.three.position.set(-9.5, 0, 9.5); ground.add(boxingRing.four); boxingRing.beams.one.rotation.set(Math.PI/2, 0, Math.PI/4); boxingRing.four.position.set(9.5, 0, 9.5); ground.add(boxingRing.beams.one); boxingRing.beams.one.position.set(0, 2, 0); boxingRing.beams.two.rotation.set(Math.PI/2, 0, Math.PI/4); ground.add(boxingRing.beams.two); boxingRing.beams.two.position.set(0, 3, 0); boxingRing.beams.three.rotation.set(Math.PI/2, 0, Math.PI/4); ground.add(boxingRing.beams.three); boxingRing.beams.three.position.set(0, 4, 0); scene.add(ground); function GameLoop(){ requestAnimationFrame(GameLoop); PlayerControl(); CameraControl(); Render(); lightHolder.rotation.x += 0.01; } function Render(){ renderer.render(scene, camera); } function CameraControl(){ var target = new THREE.Vector3( (player1.body.position.x + player2.body.position.x)/2, (player1.body.position.y + player2.body.position.y)/2, (player1.body.position.z + player2.body.position.z)/2 ); camera.lookAt(target); var dist = Math.sqrt(Math.pow(player1.body.position.x - player2.body.position.x, 2) + Math.pow(player1.body.position.z - player2.body.position.z, 2)); camera.fov = dist*5; camera.updateProjectionMatrix(); } function PlayerControl(){ player1.body.lookAt(player2.body.position); player2.body.lookAt(player1.body.position); Anim1(); Anim2(); } var animations = [ [//0 = charge, 1 = shoot, 2 = block// //Left Hand Pos { x: [2.5, 2.5, 2.5, 2.5], y: [1, 1, 1, 1], z: [0.5, 0.5, 0.5, 0.5] }, //Right Hand Pos { x: [-2.5, -2.5, -2.5, -2.5], y: [1, 1, 1, 1], z: [0.5, 2, 0.5, 0.5] }, //Left Hand Rot { x: [0, 0, 0, 0], y: [0, 0, 0, 0], z: [0, 0, 0, 0] }, //Right Hand Rot { x: [0, 0, -70, -70], y: [0, 0, 0, 0], z: [0, 0, 0, 0] }, //Shield Scale { x: [0, 0, 0, 0], y: [0, 0, 0, 0], z: [0, 0, 0, 0] }, //Gun Scale { x: [0, 1, 1, 1], y: [0, 1, 1, 1], z: [0, 1, 1, 1] } ], [ //Left Hand Pos { x: [2.5, 0.25, 0.25, 0.25], y: [1, 1.5, 1.5, 1.5], z: [0.5, 2.5, 1.5, 2.5] }, //Right Hand Pos { x: [-2.5, -0.25, -0.25, -0.25], y: [1, 1.5, 1.5, 1.5], z: [0.5, 2.5, 1.5, 2.5] }, //Left Hand Rot { x: [0, 0, 0, 0], y: [0, 0, 0, 0], z: [0, 0, 0, 0] }, //Right Hand Rot { x: [0, 0, 0, 0], y: [0, 0, 0, 0], z: [0, 0, 0, 0] }, //Shield Scale { x: [0, 0, 0, 0], y: [0, 0, 0, 0], z: [0, 0, 0, 0] }, //Gun Scale { x: [0, 0, 1, 1], y: [0, 0, 1, 1], z: [0, 0, 1, 1] } ], [ //Left Hand Pos { x: [1.5, 0.25, 0.25, 0.25], y: [1, 1, 1, 1], z: [0.5, 2, 2, 2] }, //Right Hand Pos { x: [-1.5, -0.25, -0.25, -0.25], y: [1, 1, 1, 1], z: [0.5, 2, 2, 2] }, //Left Hand Rot { x: [0, 0, 0, 0], y: [0, 0, 0, 0], z: [0, 0, 0, 0] }, //Right Hand Rot { x: [0, 0, 0, 0], y: [0, 0, 0, 0], z: [0, 0, 0, 0] }, //Shield Scale { x: [0, 2.5, 2.5, 2.5], y: [0, 2.5, 2.5, 2.5], z: [0, 2.5, 2.5, 2.5] }, //Gun Scale { x: [0, 0, 0, 0], y: [0, 0, 0, 0], z: [0, 0, 0, 0] } ]]; var frame1 = 1; var lastFrame1 = 0; var time1 = 0; var clip1 = 0; var lastClip1 = 0; var speed = 10; function Anim1(){ time1++; if (time1 == speed){ time1 = 0; lastFrame1 = frame1; frame1++; if (frame1 == 4){ frame1 = 0; lastClip1 = clip1; clip1++; if (clip1 == sequence1.length){ clip1 = 0; } } } if (frame1 !== 0){ lastClip1 = clip1; } var values = { leftPos: { x: ((animations[sequence1[clip1]][0].x[frame1]/speed*time1) + (animations[sequence1[lastClip1]][0].x[lastFrame1]/speed*(speed-time1)))/2, y: ((animations[sequence1[clip1]][0].y[frame1]/speed*time1) + (animations[sequence1[lastClip1]][0].y[lastFrame1]/speed*(speed-time1)))/2, z: ((animations[sequence1[clip1]][0].z[frame1]/speed*time1) + (animations[sequence1[lastClip1]][0].z[lastFrame1]/speed*(speed-time1)))/2 }, rightPos: { x: ((animations[sequence1[clip1]][1].x[frame1]/speed*time1) + (animations[sequence1[lastClip1]][1].x[lastFrame1]/speed*(speed-time1)))/2, y: ((animations[sequence1[clip1]][1].y[frame1]/speed*time1) + (animations[sequence1[lastClip1]][1].y[lastFrame1]/speed*(speed-time1)))/2, z: ((animations[sequence1[clip1]][1].z[frame1]/speed*time1) + (animations[sequence1[lastClip1]][1].z[lastFrame1]/speed*(speed-time1)))/2 }, leftRot: { x: ((animations[sequence1[clip1]][2].x[frame1]/speed*time1) + (animations[sequence1[lastClip1]][2].x[lastFrame1]/speed*(speed-time1)))/2*Math.PI/90, y: ((animations[sequence1[clip1]][2].y[frame1]/speed*time1) + (animations[sequence1[lastClip1]][2].y[lastFrame1]/speed*(speed-time1)))/2*Math.PI/90, z: ((animations[sequence1[clip1]][2].z[frame1]/speed*time1) + (animations[sequence1[lastClip1]][2].z[lastFrame1]/speed*(speed-time1)))/2*Math.PI/90 }, rightRot: { x: ((animations[sequence1[clip1]][3].x[frame1]/speed*time1) + (animations[sequence1[lastClip1]][3].x[lastFrame1]/speed*(speed-time1)))/2*Math.PI/90, y: ((animations[sequence1[clip1]][3].y[frame1]/speed*time1) + (animations[sequence1[lastClip1]][3].y[lastFrame1]/speed*(speed-time1)))/2*Math.PI/90, z: ((animations[sequence1[clip1]][3].z[frame1]/speed*time1) + (animations[sequence1[lastClip1]][3].z[lastFrame1]/speed*(speed-time1)))/2*Math.PI/90 }, shieldScale: { x: ((animations[sequence1[clip1]][4].x[frame1]/speed*time1) + (animations[sequence1[lastClip1]][4].x[lastFrame1]/speed*(speed-time1)))/2, y: ((animations[sequence1[clip1]][4].y[frame1]/speed*time1) + (animations[sequence1[lastClip1]][4].y[lastFrame1]/speed*(speed-time1)))/2, z: ((animations[sequence1[clip1]][4].z[frame1]/speed*time1) + (animations[sequence1[lastClip1]][4].z[lastFrame1]/speed*(speed-time1)))/2 }, gunScale: { x: ((animations[sequence1[clip1]][5].x[frame1]/speed*time1) + (animations[sequence1[lastClip1]][5].x[lastFrame1]/speed*(speed-time1)))/2, y: ((animations[sequence1[clip1]][5].y[frame1]/speed*time1) + (animations[sequence1[lastClip1]][5].y[lastFrame1]/speed*(speed-time1)))/2, z: ((animations[sequence1[clip1]][5].z[frame1]/speed*time1) + (animations[sequence1[lastClip1]][5].z[lastFrame1]/speed*(speed-time1)))/2 } }; player1.hands.l.position.set( values.leftPos.x, values.leftPos.y, values.leftPos.z ); player1.hands.r.position.set( values.rightPos.x, values.rightPos.y, values.rightPos.z ); player1.hands.l.rotation.set( values.leftRot.x, values.leftRot.y, values.leftRot.z ); player1.hands.r.rotation.set( values.rightRot.x, values.rightRot.y, values.rightRot.z ); player1.shield.scale.set( values.shieldScale.x, values.shieldScale.y, values.shieldScale.z ); player1.pistol.scale.set( values.gunScale.x, values.gunScale.y, values.gunScale.z ); } var frame2 = 1; var lastFrame2 = 0; var time2 = 0; var clip2 = 0; var lastClip2 = 0; var speed = 10; function Anim2(){ time2++; if (time2 == speed){ time2 = 0; lastFrame2 = frame2; frame2++; if (frame2 == 4){ frame2 = 0; lastClip2 = clip2; clip2++; if (clip2 == sequence2.length){ clip2 = 0; } } } if (frame2 !== 0){ lastClip2 = clip2; } var values = { leftPos: { x: ((animations[sequence2[clip2]][0].x[frame2]/speed*time2) + (animations[sequence2[lastClip2]][0].x[lastFrame2]/speed*(speed-time2)))/2, y: ((animations[sequence2[clip2]][0].y[frame2]/speed*time2) + (animations[sequence2[lastClip2]][0].y[lastFrame2]/speed*(speed-time2)))/2, z: ((animations[sequence2[clip2]][0].z[frame2]/speed*time2) + (animations[sequence2[lastClip2]][0].z[lastFrame2]/speed*(speed-time2)))/2 }, rightPos: { x: ((animations[sequence2[clip2]][1].x[frame2]/speed*time2) + (animations[sequence2[lastClip2]][1].x[lastFrame2]/speed*(speed-time2)))/2, y: ((animations[sequence2[clip2]][1].y[frame2]/speed*time2) + (animations[sequence2[lastClip2]][1].y[lastFrame2]/speed*(speed-time2)))/2, z: ((animations[sequence2[clip2]][1].z[frame2]/speed*time2) + (animations[sequence2[lastClip2]][1].z[lastFrame2]/speed*(speed-time2)))/2 }, leftRot: { x: ((animations[sequence2[clip2]][2].x[frame2]/speed*time2) + (animations[sequence2[lastClip2]][2].x[lastFrame2]/speed*(speed-time2)))/2*Math.PI/90, y: ((animations[sequence2[clip2]][2].y[frame2]/speed*time2) + (animations[sequence2[lastClip2]][2].y[lastFrame2]/speed*(speed-time2)))/2*Math.PI/90, z: ((animations[sequence2[clip2]][2].z[frame2]/speed*time2) + (animations[sequence2[lastClip2]][2].z[lastFrame2]/speed*(speed-time2)))/2*Math.PI/90 }, rightRot: { x: ((animations[sequence2[clip2]][3].x[frame2]/speed*time2) + (animations[sequence2[lastClip2]][3].x[lastFrame2]/speed*(speed-time2)))/2*Math.PI/90, y: ((animations[sequence2[clip2]][3].y[frame2]/speed*time2) + (animations[sequence2[lastClip2]][3].y[lastFrame2]/speed*(speed-time2)))/2*Math.PI/90, z: ((animations[sequence2[clip2]][3].z[frame2]/speed*time2) + (animations[sequence2[lastClip2]][3].z[lastFrame2]/speed*(speed-time2)))/2*Math.PI/90 }, shieldScale: { x: ((animations[sequence2[clip2]][4].x[frame2]/speed*time2) + (animations[sequence2[lastClip2]][4].x[lastFrame2]/speed*(speed-time2)))/2, y: ((animations[sequence2[clip2]][4].y[frame2]/speed*time2) + (animations[sequence2[lastClip2]][4].y[lastFrame2]/speed*(speed-time2)))/2, z: ((animations[sequence2[clip2]][4].z[frame2]/speed*time2) + (animations[sequence2[lastClip2]][4].z[lastFrame2]/speed*(speed-time2)))/2 }, gunScale: { x: ((animations[sequence2[clip2]][5].x[frame2]/speed*time2) + (animations[sequence2[lastClip2]][5].x[lastFrame2]/speed*(speed-time2)))/2, y: ((animations[sequence2[clip2]][5].y[frame2]/speed*time2) + (animations[sequence2[lastClip2]][5].y[lastFrame2]/speed*(speed-time2)))/2, z: ((animations[sequence2[clip2]][5].z[frame2]/speed*time2) + (animations[sequence2[lastClip2]][5].z[lastFrame2]/speed*(speed-time2)))/2 } }; player2.hands.l.position.set( values.leftPos.x, values.leftPos.y, values.leftPos.z ); player2.hands.r.position.set( values.rightPos.x, values.rightPos.y, values.rightPos.z ); player2.hands.l.rotation.set( values.leftRot.x, values.leftRot.y, values.leftRot.z ); player2.hands.r.rotation.set( values.rightRot.x, values.rightRot.y, values.rightRot.z ); player2.shield.scale.set( values.shieldScale.x, values.shieldScale.y, values.shieldScale.z ); player2.pistol.scale.set( values.gunScale.x, values.gunScale.y, values.gunScale.z ); } GameLoop(); </script> </body> </html>值组成的数据集,并创建一个“典型周”的数据,这样一个(60 * 24 * 7)= 10080行。每行都有一些特定的分位数/百分位数值,例如[0.1、0.25、0.5、0.75、0.9]。分位数是根据所有历史数据集中一周中该分钟的x值以及该分钟A的前五分钟和后五分钟计算的。

因此,输出将是一周中的每一分钟,该分钟的x值以及这些数据的值,包括百分比计算中的之前和之后的分钟,例如

输入数据:

x

输出数据:

                                x    dow    hour  minute
               time                                     
2016-10-18 07:55:00     29.500000      0       7      55
2016-10-18 07:56:00     35.000000      0       7      56
2016-10-18 07:57:00     24.666667      0       7      57
2016-10-18 07:58:00     31.833333      0       7      58
2016-10-18 07:59:00     35.500000      0       7      59
2016-10-18 08:00:00     35.500000      0       8      00 
2016-10-18 08:01:00     33.500000      0       8      01
...                     ...          ...     ...     ...
2018-10-18 10:01:00     32.500000      6      10      01
2018-10-18 10:02:00     34.500000      6      10      02
2018-10-18 10:03:00     39.500000      6      10      03

我如何在大熊猫中实现这样的目标。这似乎是朝着正确方向迈出的一步,但我不确定滚动是否包括预期的前后分钟:

dow hour minute percentile1 percentile2 percentile3 percentile4 percentile5 3 4 58 25 28 33 44 50 ... ...

1 个答案:

答案 0 :(得分:1)

您可以使用

g=df.groupby(['dow', 'hour', 'minute'])['x']

s=pd.concat({y:g.apply(lambda x : x.rolling(5,min_periods=1).quantile(y)) for y in [0.25,0.30,0.50]},1)
s
Out[298]: 
        0.25       0.30       0.50
0  29.500000  29.500000  29.500000
1  35.000000  35.000000  35.000000
2  24.666667  24.666667  24.666667
3  31.833333  31.833333  31.833333
4  35.500000  35.500000  35.500000
5  33.500000  33.500000  33.500000

yourdf=pd.concat([df,s],axis=1)