我需要在B列中重复的值,直到A列发生变化。
这是输入
<!DOCTYPE html>
<html data-ng-app="myApp">
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
</head>
<body data-ng-controller="testController">
<ng-form name="phoneInnerForm">
<div>
<div class="form-group" data-ng-repeat="item in items" ng-class="phoneInnerForm.phones{{$index}}.$error.maxlength ? 'phone_number_error': ''">
<input type="text" class="form-control" id="InputAddPhone" name="phones{{$index}}" ng-model="item.number" ng-maxlength="50">
<select class="form-control" ng-model="item.type">
<option value=""></option>
<option value="mobile">Mobile</option>
<option value="work">Work</option>
<option value="home">Home</option>
<option value="fax">Fax</option>
<option value="other">Other</option>
</select>
<div class="evy_email_dltbtn">
<button type="button" class="btn btn-default btn_delete" ng-click="deleteItem($index);" title="Delete">Delete</button>
<button ng-show="$last" type="button" class="btn btn-default btn_delete" ng-click="addItem();" title="Delete">Add</button>
</div>
<span ng-show="phoneInnerForm.phones{{$index}}.$error.maxlength" class="evy_user-preference_error">Phone number should not exceed 50 characters</span>
</div>
</div>
</ng-form>
<script>
angular
.module('myApp', [])
.controller('testController', ['$scope',
function($scope) {
$scope.items = [{
number: "",
type: ""
}];
$scope.addItem = function() {
$scope.items.push({
number: "",
type: ""
});
}
$scope.deleteItem = function(index) {
$scope.items.splice(index, 1);
}
}
]);
</script>
</body>
</html>
预期输出
Column A Column B
18 1
18 0
18 0
18 0
24 2
24 0
18 3
18 0
18 0
18 0
答案 0 :(得分:5)
如果first
需要按transform
移位列Series
创建的Col A
重复每个组的第一个值,则print (df['Col A'].ne(df['Col A'].shift()).cumsum())
0 1
1 1
2 1
3 1
4 2
5 2
6 3
7 3
8 3
9 3
Name: Col A, dtype: int32
df['Col B'] = df.groupby(df['Col A'].ne(df['Col A'].shift()).cumsum())['Col B']
.transform('first')
print (df)
Col A Col B
0 18 1
1 18 1
2 18 1
3 18 1
4 24 2
5 24 2
6 18 3
7 18 3
8 18 3
9 18 3
可以使用cumsum
:< / p>
ls
答案 1 :(得分:0)
A =[18,18,18,18,24,24,18,18,18,18]
def func(A):
B = []
b = 1
for i in range(len(A)):
if i == 0:
B.append(b)
continue
if A[i] != A[i-1]:
b += 1
B.append(b)
else:
B.append(b)
return B
出:
[1, 1, 1, 1, 2, 2, 3, 3, 3, 3]