pandas - 选择带有整数列表的索引

时间:2017-04-29 17:04:03

标签: python pandas

我有一个pandas数据框,如:

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Amfphp_Core_HttpRequestGatewayFactory;
use Amfphp_Core_Config;

class AmfphpController extends Controller {

    public function amfphp() {

        $myConfig = new Amfphp_Core_Config();
        $myConfig->serviceFolders = array();
        $myConfig->serviceFolders [] = dirname(__FILE__) . '/../Services/';

        $gateway = Amfphp_Core_HttpRequestGatewayFactory::createGateway($myConfig);

        $gateway->service();
        $gateway->output();

    }
}

我想从中删除列表中包含索引的行:

import numpy as np
pd.DataFrame(np.random.rand(20,2))

我该怎么做?

2 个答案:

答案 0 :(得分:2)

使用drop

df = df.drop(list_)
print (df)
           0         1
1   0.311202  0.435528
2   0.225144  0.322580
3   0.165907  0.096357
7   0.925991  0.362254
8   0.777936  0.552340
9   0.445319  0.672854
10  0.919434  0.642704
11  0.751429  0.220296
12  0.425344  0.706789
13  0.708401  0.497214
14  0.110249  0.910790
15  0.972444  0.817364
16  0.108596  0.921172
17  0.299082  0.433137
19  0.234337  0.163187

答案 1 :(得分:1)

这样做:

remove = df.index.isin(list_)
df[~remove]

或者只是:

df[~df.index.isin(list_)]