逐步将pandas dataframe排在面板中的其他布尔数据框架中

时间:2016-08-04 04:50:00

标签: python pandas nan rank

我在面板中有两个pandas数据帧,并且想要创建第三个df,它排列第一个df(按行),但仅包括第二个df的相应元素为True的那些。一些示例数据来说明:

              A    B    C    D   E
2015-12-31  2.0  NaN  1.0  NaN NaN
2016-01-31  3.0  2.0  1.0  NaN NaN
2016-02-29  NaN  3.0  2.0  1.0 NaN
2016-03-31  NaN  NaN  NaN  NaN NaN

我已经设法用一些丑陋的黑客做到这一点,但仍然坚持排名不让我使用方法='首先'在非数字数据上。我想强制增量整数排名(即使重复)和NaN用于在布尔df中没有True的任何单元格。

输出应采用以下形式:

# I first had to hack a replacement of False with NaN.
# np.nan did not evaluate correctly
# I wasn't sure how else to specify pandas NaN
rank=p['Z'].replace(False,p['Z'].iloc[3,0])

# eliminate the elements without a corresponding True
rank=rank*p['X']

# then this works
p['rank'] = rank.rank(axis=1, ascending=False)

# but this doesn't
p['rank'] = rank.rank(axis=1, ascending=False, method='first')

我的黑客攻击在下面。虽然应该有一个更好的方法用NaN替换false,但它确实有效。但是,一旦我添加方法='首先'它就无法工作。这是必要的,因为我可能有重复值的实例。

angular.
    module('myApp').
    component('zipPopup', {
        templateUrl: 'zip-popup/zip-popup.template.html',
        controller: function ZipPopupController($scope, $http) {
            $scope.show = true;
            $scope.hide = function(){
                $scope.show = false;
            };
            $scope.user = {};
            $scope.regex = '\\d{5}';

            $scope.submitForm = function(isValid) {
                if(isValid) {
                    $http({
                        method  : 'POST',
                        url     : '/leads',
                        data    : $scope.user,
                        headers : {'Content-Type': 'application/x-www-form-urlencoded'} 
                    })
                    .success(function(response) {
                        $scope.show = false;
                    })
                    .error(function(response) {
                        console.log(response);
                    });
                }
            };
        }
    });

任何帮助将不胜感激! 感谢

  1. 列表项

1 个答案:

答案 0 :(得分:2)

pd.DataFrame(np.where(p['y'] == True, p['x'], np.nan),
             p.major_axis, p.minor_axis).rank(1, ascending=False)

enter image description here