根据列中的值,为每个值提取前N行

时间:2017-07-26 15:34:22

标签: python pandas

在python中使用pandas或其他函数,

我想为每个“名称”提取前三行(基于“权重”值):

例如,我有这样的数据。

    df = 
row weight name size1 size2 
    1 10 A 4 4
    2 7 A 2 7
    3 5 A 9 7
    4 5 A 2 2
    5 2 A 6 3
    1 7 B 3 4
    2 6 B 8 3
    3 5 B 4 3
    4 3 B 4 5
    5 2 B 2 1

我希望我的输出看起来像这样:

row weight name size1 size2
1 10 A 4 4
2 7 A 2 7
3 5 A 9 7
1 7 B 3 4
2 6 B 8 3
3 5 B 4 3

(when 3rd, 4th "weight" value are same)
row weight name size1 size2
1 10 A 4 4
2 7 A 2 7
3 5 A 9 7
4 5 A 2 2
1 7 B 3 4
2 6 B 8 3
3 5 B 4 3

1 个答案:

答案 0 :(得分:1)

试试这个:

public function behaviors()
    {
        $behaviors = parent::behaviors();


        // add CORS filter
        $behaviors['corsFilter'] = [
            'class' => \yii\filters\Cors::className(),
            'cors' => [
                'Origin' => ['*'],
                'Access-Control-Request-Method' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
                'Access-Control-Request-Headers' => ['*'],
            ],

        ];
        $behaviors['contentNegotiator'] = [
            'class' => \yii\filters\ContentNegotiator::className(),
            'formats' => [
                'application/json' => \yii\web\Response::FORMAT_JSON,
            ],
        ];
        return $behaviors;
    }