python - Pandas groupby + resample/TimeGrouper 用于从开始起几个月的变化

标签 python pandas

我有一个员工薪资数据的数据框(示例如下),其中“日期”指员工薪资生效的时间:

Employee    Date        Salary
PersonA     1/1/2016    $50000 
PersonB     3/5/2014    $65000 
PersonB     3/1/2015    $75000 
PersonB     3/1/2016    $100000 
PersonC     5/15/2010   $75000 
PersonC     6/3/2011    $100000 
PersonC     3/10/2012   $110000 
PersonC     9/5/2012    $130000 
PersonC     3/1/2013    $150000 
PersonC     3/1/2014    $200000 

在此示例中,PersonA 今年的起薪为 50,000 美元,PersonC 已经在公司工作了一段时间,并且自 2010 年 5 月 15 日起开始多次加薪。

我需要将日期列转换为从开始算起的月份,以单个员工为基础,其中从开始算起的月份将递增m 个月(由我指定)。例如,对于 PersonB,假设 m=12,结果将是:

Employee    Months From Start   Salary
PersonB     0                   $65000 
PersonB     12                  $65000 
PersonB     24                  $75000 

这意味着在第 0 个月(就业开始),PersonB 的工资为 65,000 美元; 12 个月后,他的工资为 65,000 美元,24 个月后,他的工资为 75,000 美元。请注意,下一个增量(36 个月)将不会出现在 PersonB 的转换数据帧上,因为该持续时间超过了 PersonB 的就业持续时间(将在未来)。

再次注意,我希望能够将 m 调整为任何月份增量。如果我想要 6 个月的增量 (m=6),结果将是:

Employee    Months From Start   Salary
PersonB     0                   $65000 
PersonB     6                   $65000 
PersonB     12                  $65000 
PersonB     18                  $75000 
PersonB     24                  $100000 
PersonB     30                  $100000 

作为最后一步,我还想将员工截至今天的工资包含在转换后的数据框中。再次使用 PersonB,并假设 m=6,这意味着结果将是:

Employee    Months From Start   Salary
PersonB     0                   $65000 
PersonB     6                   $65000 
PersonB     12                  $65000 
PersonB     18                  $75000 
PersonB     24                  $100000 
PersonB     30                  $100000 
PersonB     32.92               $100000 <--added (today is 32.92 months from start)

问题:是否有一种编程方式(我假设至少使用以下之一:groupbyresampleTimeGrouper)来实现上述所需的数据帧?

注意:您可以假设所有员工都在职(没有离开公司)。

最佳答案

您可以结合 group_by 和 resample 来完成此操作。要使用重新采样,您需要将日期作为索引。

df.index = pd.to_datetime(df.Date)
df.drop('Date',axis = 1, inplace = True)

然后:

df.groupby('Employee').resample('6m').pad()

在本例中,我使用 6 个月的周期。请注意,它将是每个月的最后一天,我希望这不会成为问题。 然后你将拥有:

    Employee   Date      Salary
0   PersonA 2016-01-31   $50000
1   PersonB 2014-03-31   $65000
2   PersonB 2014-09-30   $65000
3   PersonB 2015-03-31   $75000
4   PersonB 2015-09-30   $75000
5   PersonB 2016-03-31  $100000
6   PersonC 2010-05-31   $75000
7   PersonC 2010-11-30   $75000
8   PersonC 2011-05-31   $75000
9   PersonC 2011-11-30  $100000
10  PersonC 2012-05-31  $110000
11  PersonC 2012-11-30  $130000
12  PersonC 2013-05-31  $150000
13  PersonC 2013-11-30  $150000
14  PersonC 2014-05-31  $200000

现在您可以创建“自开始以来的月份”列(cumcount 函数检查每行在其组中出现的顺序)。请记住将其乘以每个期间使用的月数(在本例中为 6):

df['Months since started'] = df.groupby('Employee').cumcount()*6

     Employee   Date      Salary     Months since started
0   PersonA 2016-01-31   $50000                  0
1   PersonB 2014-03-31   $65000                  0
2   PersonB 2014-09-30   $65000                  6
3   PersonB 2015-03-31   $75000                 12
4   PersonB 2015-09-30   $75000                 18
5   PersonB 2016-03-31  $100000                 24
6   PersonC 2010-05-31   $75000                  0
7   PersonC 2010-11-30   $75000                  6
8   PersonC 2011-05-31   $75000                 12
9   PersonC 2011-11-30  $100000                 18
10  PersonC 2012-05-31  $110000                 24
11  PersonC 2012-11-30  $130000                 30
12  PersonC 2013-05-31  $150000                 36
13  PersonC 2013-11-30  $150000                 42
14  PersonC 2014-05-31  $200000                 48

希望对您有帮助!

关于python - Pandas groupby + resample/TimeGrouper 用于从开始起几个月的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40917935/

相关文章:

Python:如何用离散(分箱)值替换数组,而不是分箱编号?

python - WTForms 如何使用 DecimalField 在错误输入时生成正确的字段错误

python - 使用 Matplotlib 的 3D 绘图 : Hide axes but keep axis-labels?

python - 使用 pandas GroupBy 聚合时设置 MultiIndex

python - 按一年中的某天分组

python - 告诉 Flask 使用哪个虚拟环境

python - 尝试通过 Buildout 在 Ubuntu 上安装 PyCrypto,src/config.h : No such file or directory

python - pandas : how to apply scipy. 对 groupby 对象的统计测试?

python - 将索引号转换为 int (Python)

python - 如何将 pip 命令的结果存储到 Pandas Dataframe 中