Python根据角色数据库计算管理权 [英] Python calculate management tenure based on role database
本文介绍了Python根据角色数据库计算管理权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个角色数据库(如pd数据框),该数据库具有以下变量:个人,公司和年份-每个公司年份组合中只有一个人。
I have a role database (as pd dataframe) that has the following variables: individual, company and year - with only one individual for every company-year combination.
现在,我想用每个唯一的公司作为索引来创建一个新的数据框,并计算最近一个人在公司中呆了多长时间。
Now, I want to create a new dataframe with every unique company as the index and calculate, how long the most recent individual has been in the company.
例如以下内容在新数据框中的输出应为A,4和B,2 。
import pandas as pd
d = {'Individual_ID': [1,1,1,1,2,2,2,3,3,4,4,4,4,4],
'Company': ['A','A','A','A','A','A','A','B','B','B','B','B','B','B'],
'Year':[2016,2015,2014,2013,2012,2011,2010,2016,2015,2014,2013,2012,2011,2010]}
df = pd.DataFrame(data=d)
df
推荐答案
如果按公司
排序 Individual_ID
,则可以使用:
You can use if Individual_ID
are sorted per Company
:
df = df.groupby('Company')['Individual_ID'].apply(lambda x: (x == x.iat[0]).sum())
print (df)
Company
A 4
B 2
Name: Individual_ID, dtype: int64
这篇关于Python根据角色数据库计算管理权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文