Python Pandas:有条件地删除行

时间:2017-07-17 18:56:32

标签: python pandas

enter image description here

enter image description here

您好, 我想删除列col1和col2中的值都为0的行。

现在,我有:

df = df[df['col1'] != 0 and df['col2'] != 0]

但是我收到一条错误消息: TypeError:无法将dtyped [float64]数组与[bool]类型的标量进行比较

任何人都可以解释一下如何实现这个PLZ吗?

1 个答案:

答案 0 :(得分:3)

它被称为boolean indexing

您需要按位df = df[(df['col1'] != 0) & (df['col2'] != 0)] 并为优先级运算符添加括号:

df = df.query("'col1' != 0 & 'col2' != 0")

或者:

function Service1(Service2, $http, $q) {

    var eventDataService = {};

    var endPointUrl = $q.defer();
    Service2.getConfig().then(function (config) {
        endPointUrl.resolve(config.endPointUrl);
    });

    eventDataService.getCount = getCount;
    return eventDataService;

    function getEndPointUrl() {
        return endPointUrl.promise;
    }

    function getCount(successCallback, failureCallback) {
        getEndPointUrl().then(function (endPointUrl) {
            var uri = endPointUrl + '/event/gunshotCount';
            $http.get(uri).then(function (data) {
                successCallback(data);
            }, function (error) {
                failureCallback(error);
            });
        })
            .catch(function (error) {
                $log.error("Error while getting REST endpoint Url. Error : " + error);
            });
    }
}