比较pandas Dataframe列的值是逐个的

时间:2018-01-05 06:35:21

标签: python pandas dataframe compare

DF

        Date    Factory I1  D1  I2  D2  I3  D3

   0    1701    West    0   0   0   0   10  0

   1    1701    East    39  0   83  15  3   15

   2    1701    West    0   0   0   10  15  0

我想要这个:

 If D2>I2 Then I3=I2+I3,D3=D2+D3,I2=D2=0

 If D3>I3 Then I2=I2+I3,D2=D2+D3,I3=D3=0

否则没有变化

所以数据应该是这样的:

DF

     Date   Factory I1  D1  I2  D2  I3  D3

     0  1701    West    0   0   0   0   10  0

     1  1701    East    39  0   86  30  0   0

     2  1701    West    0   0   0   0   15  10

我该怎么办?请帮忙〜

2 个答案:

答案 0 :(得分:2)

mask需要assign

    <div ng-controller="AppKeysCtrl">
<button ng-click="add()">
Add
</button>
<table class="table table-hover mytable">
    <thead>
        <tr>
            <th></th>
            <th>Created</th>
            <th>App Key</th>
            <th>Name</th>
            <th>Level</th>
 http://jsfiddle.net/Thw8n/155/#update           <th>Active</th>
            <th>Edit</th>
        </tr>
    </thead>
    <tbody>
        <tr data-ng-repeat="entry in appkeys" data-ng-class="{danger:!entry.active}">
        <td>{{$index + 1}}</td>
        <td>{{entry.timestamp | date:'mediumDate'}}</td>
        <td>{{entry.appkey}}</td>
        <td>
            <span data-ng-hide="editMode">{{entry.name}}</span>
            <input type="text" data-ng-show="editMode" data-ng-model="entry.name" data-ng-required />
        </td>
        <td>
            <span data-ng-hide="editMode">{{entry.level}}</span>
            <select class="form-control" name="entry.level" data-ng-model="entry.level" data-ng-show="editMode">
                <option value="3">3 - Developer Access - [Temporary]</option>
                <option value="2">2 - Standard Tool Access - [Default]</option>
                <option value="1">1 - Administrative Access - [Admin Section Only]</option>
            </select>
        </td>
        <td>
            <span data-ng-hide="editMode">{{entry.active && 'Active' || 'Inactive'}}</span>
            <select class="form-control" name="entry.active" data-ng-model="entry.active" data-ng-show="editMode">
                <option value="true">Active</option>
                <option value="false">Inactive</option>
            </select>
        </td>
        <td>
            <button type="submit" data-ng-hide="editMode" data-ng-click="editMode = true; editAppKey(entry)" class="btn btn-default">Edit</button>
            <button type="submit" data-ng-show="editMode" data-ng-click="editMode = false" class="btn btn-default">Save</button>
            <button type="submit" data-ng-show="editMode" data-ng-click="editMode = false; cancel($index)" class="btn btn-default">Cancel</button>
        </td>
</tr>
</tbody>
</table>
    <pre>newField: {{newField|json}}</pre></br></br>
    <pre>appkeys: {{appkeys|json}}</pre>
</div>

app = angular.module("formDemo", []);

function AppKeysCtrl($scope, $http, $location) {
    var tmpDate = new Date();

          $scope.newField = [];
          $scope.editing = false;

     $scope.appkeys = [
         { "appkey" : "0123456789", "name" : "My new app key", "created" : tmpDate },
         { "appkey" : "abcdefghij", "name" : "Someone elses app key", "created" : tmpDate }
     ];

    $scope.editAppKey = function(field) {
        $scope.editing = $scope.appkeys.indexOf(field);
        $scope.newField[$scope.editing] = angular.copy(field);
    }

    $scope.saveField = function(index) {
        //if ($scope.editing !== false) {
            $scope.appkeys[$scope.editing] = $scope.newField;
            //$scope.editing = false;
        //}       
   };

    $scope.cancel = function(index) {
        //if ($scope.editing !== false) {
            $scope.appkeys[index] = $scope.newField[index];
            $scope.editing = false;
        //}       
    };

    $scope.add = function () {
            var entry = {};
            //$scope.goals.push(goal);
            $scope.appkeys.push(entry);
        };
}

angular.element(document).ready(function() {
    angular.bootstrap(document, ["formDemo"]);
});

如果列中包含空格:

m1 = df.D2 > df.I2
m2 = df.D3 > df.I3
df = df.mask(m1, df.assign(I3=df.I2+df.I3, D3=df.D2+df.D3, I2=0, D2=0))
df = df.mask(m2, df.assign(I2=df.I2+df.I3, D2=df.D2+df.D3, I3=0, D3=0))
print (df)
   Date Factory  I1  D1  I2  D2  I3  D3
0  1701    West   0   0   0   0  10   0
1  1701    East  39   0  86  30   0   0
2  1701    West   0   0   0   0  15  10

或者如果想要使用m1 = df.D2 > df.I2 m2 = df.D3 > df.I3 def f1(x): df['I3']=df.I2+df.I3 df['D3']=df.D2+df.D3 df['I2'] = df['D2'] = 0 return df def f2(x): df['I2']=df.I2+df.I3 df['D2']=df.D2+df.D3 df['I3'] = df['D3'] = 0 return df df = df.mask(m1, f1) df = df.mask(m2, f2) print (df) Date Factory I1 D1 I2 D2 I3 D3 0 1701 West 0 0 10 0 0 0 1 1701 East 39 0 86 30 0 0 2 1701 West 0 0 15 10 0 0 assign已更改为I3):

I 3

答案 1 :(得分:0)

可以使用易于理解的for循环并处理每一行:

newvals = []
for i in range(len(df.index)):
    ROW = df.iloc[i,:]
    if ROW.D2>ROW.I2:
        ROW.I3=ROW.I2+ROW.I3
        ROW.D3=ROW.D2+ROW.D3
        ROW.I2=ROW.D2=0 
    if ROW.D3>ROW.I3:
        ROW.I2=ROW.I2+ROW.I3
        ROW.D2=ROW.D2+ROW.D3
        ROW.I3=ROW.D3=0
    newvals.append(list(ROW))
newdf = pd.DataFrame(data=newvals, columns=df.columns)
print(newdf)

输出:

   Date Factory  I1  D1  I2  D2  I3  D3
0  1701    West   0   0   0   0  10   0
1  1701    East  39   0  86  30   0   0
2  1701    West   0   0   0   0  15  10