Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用diff()函数识别Pandas中的薪资变化以进行人力资源分析?_Python_Python 3.x_Pandas_Difference - Fatal编程技术网

Python 如何使用diff()函数识别Pandas中的薪资变化以进行人力资源分析?

Python 如何使用diff()函数识别Pandas中的薪资变化以进行人力资源分析?,python,python-3.x,pandas,difference,Python,Python 3.x,Pandas,Difference,给定一个包含等级和工资的HR员工数据集,我想确定每个员工的等级和工资是否都有变化 我可以使用.diff()函数来完成,但是当第二个员工进来时,它会获取最后一个员工数据,这不是我所期望的。我希望为每个员工使用.diff()函数或其他方法 下面是迄今为止使用的代码 import pandas as pd # This is my Dataset hr = pd.DataFrame({'Employee': ['100201', '100201', '100201', '100201', '100

给定一个包含等级和工资的HR员工数据集,我想确定每个员工的等级和工资是否都有变化

我可以使用.diff()函数来完成,但是当第二个员工进来时,它会获取最后一个员工数据,这不是我所期望的。我希望为每个员工使用.diff()函数或其他方法

下面是迄今为止使用的代码

import pandas as pd

# This is my Dataset
hr = pd.DataFrame({'Employee': ['100201', '100201', '100201', 
'100201', '100201', '100201','100299', '100299'],
                   'Month/Year': ['01.2018', '02.2018', '03.2018', 
'04.2018', '05.2018', '06.2018','01.2019', '02.2019'],
                   'Salary': [12175, 13000, 13000, 13125, 14000, 
14000, 20000, 21000],
                   'Grade': [1, 1, 2, 2, 2, 1, 3, 4],
                   'Position': [1, 1, 2, 2, 2, 2, 3, 4]})

hr

# This is how I check the diff from each month:
hr.set_index('Employee')
hr['Increase'] = hr['Salary'].diff(1)
hr['Grade Change'] = hr['Grade'].diff(1)
hr

# Finally just apply a lambda function
hr['Promotion'] = hr['Increase'].apply(lambda x: x > 0 )
hr['Grade Increase'] = hr['Grade Change'].apply(lambda x: x != 0 )
hr
从结果中可以看出:

我能够理解员工100201的所有等级和工资变化。然而,对于员工100299,代码从员工100299的索引5中获取14000的工资,因此提到有6000个工资变化。事实上,100299员工仅在2019年1月加入,起薪为20000。2019年2月的工资变动是正确的

我真正期望的是,每当数据集中有新员工时,都能做一些休息

我对蟒蛇和熊猫还不熟悉,所以这会有很大帮助。提前谢谢

与groupby一起使用
“员工”

hr[['Salary_increase', 'Grade_change']] = hr.groupby('Employee')[['Salary', 'Grade']].diff()
hr[['Promotion', 'Grade_increase']] =  hr[['Salary', 'Grade']].diff().gt(0)
[外]


非常感谢你!这正是我需要的。很好用!
  Employee Month/Year  Salary  Grade  Position  Salary_increase  Grade_change  \
0   100201    01.2018   12175      1         1              NaN           NaN   
1   100201    02.2018   13000      1         1            825.0           0.0   
2   100201    03.2018   13000      2         2              0.0           1.0   
3   100201    04.2018   13125      2         2            125.0           0.0   
4   100201    05.2018   14000      2         2            875.0           0.0   
5   100201    06.2018   14000      1         2              0.0          -1.0   
6   100299    01.2019   20000      3         3              NaN           NaN   
7   100299    02.2019   21000      4         4           1000.0           1.0   

   Promotion  Grade_increase  
0      False           False  
1       True           False  
2      False            True  
3       True           False  
4       True           False  
5      False           False  
6       True            True  
7       True            True