基于先前值Python求平均值

时间:2018-11-16 00:23:41

标签: python python-3.x python-2.7 pandas dataframe

我有一个看起来像这样的数据框:

@keyframes drive-truck {
  0% {
    left: -210px;
  }
  70% {
    left: 70%;
  }
  90% {
    left: 70%;
  }
  100% {
    left: 100%;
  }
}

@keyframes dump-load {
  0% {
    transform: none;
  }
  40% {
    transform: rotate(-20deg);
  }
  60% {
    transform: rotate(-20deg);
  }
  100% {
    transform: none
  }
}

#truck-container {
  width: 100%;
  overflow: hidden;
}

#truck-container .wrapper {
  position: relative;
  top: 0;
  /* left: -210px; */
  width: 200px;
  /* animation: drive-truck 5s linear infinite; */
}

#truck-container object {
  width: 200px;
}

#truck-container svg {
  width: 200px;
}

g.truck-bed {
  transform-origin: 14.3% 100%;
  transform-box: fill-box;
  animation: dump-load 5s linear infinite;
}

我想查看<div id="truck-container"> <div class="wrapper"> <!-- <object type="image/svg+xml" data="full-truck-plain.svg"></object> --> <svg xmlns:osb="http://www.openswatchbook.org/uri/2009/osb" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="svg8" version="1.1" viewBox="0 0 210.26479 80.152405" height="80.152405mm" width="210.26479mm"> <defs id="defs2"> <marker style="overflow:visible" id="Arrow1Lend" refX="0" refY="0" orient="auto"> <path transform="matrix(-0.8,0,0,-0.8,-10,0)" style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" d="M 0,0 5,-5 -12.5,0 5,5 Z" id="path6731" /> </marker> <marker style="overflow:visible" id="Arrow1Lstart" refX="0" refY="0" orient="auto"> <path transform="matrix(0.8,0,0,0.8,10,0)" style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1.00000003pt;stroke-opacity:1" d="M 0,0 5,-5 -12.5,0 5,5 Z" id="path6728" /> </marker> <linearGradient id="wheel-3"> <stop style="stop-color:#090000;stop-opacity:0;" offset="0" id="stop6086" /> <stop id="stop6098" offset="0" style="stop-color:#090000;stop-opacity:0.49803922;" /> <stop style="stop-color:#090000;stop-opacity:1;" offset="1" id="stop6084" /> </linearGradient> <linearGradient osb:paint="solid" id="linearGradient6054"> <stop id="stop6052" offset="0" style="stop-color:#000000;stop-opacity:1;" /> </linearGradient> <linearGradient osb:paint="solid" id="linearGradient6032"> <stop id="stop6030" offset="0" style="stop-color:#000000;stop-opacity:1;" /> </linearGradient> <linearGradient osb:paint="solid" id="linearGradient5958"> <stop id="stop5956" offset="0" style="stop-color:#4d4d4d;stop-opacity:1;" /> </linearGradient> <linearGradient id="linearGradient5950"> <stop id="stop5948" offset="0" style="stop-color:#000000;stop-opacity:1;" /> </linearGradient> <radialGradient gradientTransform="matrix(0.5,0,0,0.5,-129.99981,138.49981)" gradientUnits="userSpaceOnUse" r="10" fy="277" fx="200" cy="277" cx="200" id="radialGradient6090" xlink:href="#wheel-3" /> <radialGradient r="10" fy="277" fx="200" cy="277" cx="200" gradientTransform="matrix(0.5,0,0,0.5,-159.99981,138.49981)" gradientUnits="userSpaceOnUse" id="radialGradient6722" xlink:href="#wheel-3" /> <radialGradient r="10" fy="277" fx="200" cy="277" cx="200" gradientTransform="matrix(0.5,0,0,0.5,-309.99981,138.49981)" gradientUnits="userSpaceOnUse" id="radialGradient6726" xlink:href="#wheel-3" /> </defs> <metadata id="metadata5"> <rdf:RDF> <cc:Work rdf:about=""> <dc:format>image/svg+xml</dc:format> <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> <dc:title></dc:title> </cc:Work> </rdf:RDF> </metadata> <g style="display:inline" transform="translate(-9.8677083,-206.84723)" id="layer1"> <path id="rect6676" d="m 209.87461,266.99314 v 0.13643 a 9.8768147,9.8768147 0 0 0 -9.75134,9.8702 9.8768147,9.8768147 0 0 0 5.3e-4,0.007 H 69.87697 a 9.8768147,9.8768147 0 0 0 0,-0.007 9.8768147,9.8768147 0 0 0 -9.87691,-9.8764 9.8768147,9.8768147 0 0 0 -9.87692,9.8764 9.8768147,9.8768147 0 0 0 5.3e-4,0.007 H 39.87675 a 9.8768147,9.8768147 0 0 0 0,-0.007 9.8768147,9.8768147 0 0 0 -9.75134,-9.87175 v -0.13488 z" style="display:inline;fill:#4d4d4d;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:0.5;stroke-miterlimit:4.01249981;stroke-dasharray:none;stroke-opacity:1" /> <circle r="9.8768148" cy="276.99982" cx="-209.99982" id="circle6724" style="display:inline;opacity:1;fill:url(#radialGradient6726);fill-opacity:1;fill-rule:nonzero;stroke:#090000;stroke-width:0.5;stroke-miterlimit:4.01249981;stroke-dasharray:none;stroke-opacity:0" transform="scale(-1,1)" /> <circle style="display:inline;opacity:1;fill:url(#radialGradient6722);fill-opacity:1;fill-rule:nonzero;stroke:#090000;stroke-width:0.5;stroke-miterlimit:4.01249981;stroke-dasharray:none;stroke-opacity:0" id="circle6720" cx="-59.999817" cy="276.99982" r="9.8768148" transform="scale(-1,1)" /> <circle r="9.8768148" cy="276.99982" cx="-29.999817" id="circle6655" style="display:inline;opacity:1;fill:url(#radialGradient6090);fill-opacity:1;fill-rule:nonzero;stroke:#090000;stroke-width:0.5;stroke-miterlimit:4.01249981;stroke-dasharray:none;stroke-opacity:0" transform="scale(-1,1)" /> <path id="path6058" d="m 30,268 c 0,-6 0,-6 0,-6 z" style="display:inline;fill:none;stroke:#000000;stroke-width:3;stroke-linecap:square;stroke-linejoin:miter;stroke-miterlimit:4.01249981;stroke-dasharray:none;stroke-opacity:1;paint-order:markers stroke fill" /> </g> <g transform="translate(-9.8677083,-119.84723)" style="display:inline" id="layer2"> <path id="path6679" d="m 220,180 v -30 h -30 l -10,-20 h -20 v 50 h 40 c 5,-5 15,-5 20,0 z" style="display:inline;fill:#b6b6b6;fill-opacity:1;stroke:#000000;stroke-width:0.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4.01249981;stroke-dasharray:none;stroke-opacity:1" /> <path id="path5324" d="m 185,150 h -20 v -15 h 12.5 z" style="display:inline;fill:#286ad5;fill-opacity:1;stroke:#000000;stroke-width:0.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4.01249981;stroke-dasharray:none;stroke-opacity:1" /> </g> <g class="truck-bed" transform="translate(-9.8677083,-119.84723)" style="display:inline" id="layer3"> <path id="path5327" d="m 140,175 v -35 h 10 V 120 L 10,140 v 35 z" style="fill:#ea7e14;fill-opacity:1;stroke:#000000;stroke-width:0.5;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4.01249981;stroke-dasharray:none;stroke-opacity:1" /> </g> </svg> </div> </div>的每个条目,对先前的值求平均值,然后在value 1 value 2 1 10 4 1 5 8 6 10 10 12 旁边用平均值创建一个新列。

输出需要如下所示:

value 1

我该怎么做?

1 个答案:

答案 0 :(得分:3)

shift

您可以将序列的本身的移位版本加起来:

df['avg'] = (df['value1'] + df['value1'].shift()) / 2

print(df)

   value1  value2  avg
0       1      10  NaN
1       4       1  2.5
2       5       8  4.5
3       6      10  5.5
4      10      12  8.0