[英]Increment a column value based on a combination of columns
我有一个看起来像这样的数据集:
OwnerID GroupID AssignmentID ... <few more columns> [Need this column]
1 10 100 1
1 10 100 1
1 10 200 2
1 20 100 1
1 20 200 2
1 20 300 3
2 30 200 1
2 30 200 1
2 40 300 2
我想根据OwnerID
, GroupID
和AssignmentID
字段中的值填充一列。 如果这些字段中的值在行之间相同,那么我希望在新列中重复数字1
。 但是,如果同一所有者为同一组分配了不同的分配,则新列中的值应增加。
例如OwnerID
1
分配了2个分配(2个分配相同的AssignmentID
100,另一个AssignmentID
200)。 在这两种情况下, AssignmentID
100都将获得值1
,因为OwnerID
, GroupID
和AssignmentID
的值相同,但是当AssignmentID
为200时将获得值2。
同样,当OwnerID
100分配了AssignmentID
100、200和300时,这些分配所分配到的组已更改为20。
我认为可以使用以下代码完成此操作:
AssignmentDetails['colname'] = AssignmentDetails.groupby(['ownerid','groupid','assignmentid']).cumcount()
但这并没有给我所需的结果。 当“ groupby”子句中的值相同时,它不会在新列中重复该值,但会增加这些值。
我该如何实现? 任何帮助都会很棒。
df.assign(
result=df.groupby(
['OwnerID', 'GroupID']
).AssignmentID.transform(lambda x: x.factorize()[0]) + 1
)
OwnerID GroupID AssignmentID Result result
0 1 10 100 1 1
1 1 10 100 1 1
2 1 10 200 2 2
3 1 20 100 1 1
4 1 20 200 1 2
5 1 20 300 1 3
6 2 30 200 1 1
7 2 30 200 1 1
8 2 40 300 2 1
要么
df.groupby([ 'OwnerID' , 'GroupID' ]).AssignmentID.transform(lambda x: x.astype('category').cat.codes.add(1))
Out[186]:
0 1
1 1
2 2
3 1
4 2
5 3
6 1
7 1
8 1
Name: AssignmentID, dtype: int8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.