使用pandas pd.resample跟踪每个bin中的样本数量

时间:2016-08-06 16:30:47

标签: pandas

使用pandas时,是否可以跟踪每个重采样箱内的样本数量?

例如,给出样本数据:

2000-01-01 00:00:00    1
2000-01-01 00:01:00    2
2000-01-01 00:06:00    3

time_scale 5分钟重新采样时,将有2个样本用于创建第一个条形图,1个样本用于创建第二个条形图形,我想跟踪它。

我要找的结果是

index                  val  count
2000-01-01 00:00:00    1.5  2
2000-01-01 00:05:00    3.0  1

1 个答案:

答案 0 :(得分:1)

假设这是您的DataFrame:

                    foreach ($_POST['fruit'] as $selection) {

                    echo reset($selection) . " , ";
                    echo current($selection) . " , ";
                    echo end($selection) . " . ";
                }
    ?>

    <html>
       <head>
          <title>Checkbox selection using PHP (using PDO) and MySQL v2</title>
       </head>
       <body>
          <h2>Pick your most favourite fruits:</h2>
          <form name="fruitcheckbox" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
             <input type="checkbox" name="fruit[]" value="orange"> Orange
             <input type="checkbox" name="fruit[]" value="apple"> Apple
             <input type="checkbox" name="fruit[]" value="grapefruit"> Grapefruit
             <input type="checkbox" name="fruit[]" value="banana"> Banana
             <input type="checkbox" name="fruit[]" value="watermelon"> Watermelon
             <br>
             <input type="submit" value="Save" name="btn_save">
          </form>
       </body>
    </html> </span>

您可以使用.agg将多个功能应用于群组:

df
Out: 
                     C1
2000-01-01 00:00:00   1
2000-01-01 00:01:00   2
2000-01-01 00:06:00   3

df.resample('5T')['C1'].agg({'val': 'mean', 'count': 'count'}) Out: count val 2000-01-01 00:00:00 2 1.5 2000-01-01 00:05:00 1 3.0 计算每组中的观察次数,count就是他们的手段。

相关问题