回填和前填充Nans和Zeros
我正在尝试回到/前进,以填补员工的工作经验(年)。我要实现的目标是:
员工200
2019 - 3年,2018年 - 2年,2017年 - 1年
员工300
keep as Nan
Employee 400
2018 - 3年,2017年 - 2年 - 2年
员工500
2018 - 6年,2017年,2017 - 5年年,2016年至4年,
我真的很努力地以-1(+1)的增量增量将其带到回填(前填充)。如果非nan/Zero值在中间,则与员工500相比,甚至更棘手。
df_test = pd.DataFrame({'DeptID':[0,0,0,1,1,1,2,2,2],
'Employee':[200, 200, 200, 300, 400, 400, 500, 500, 500],
'Year':[2017, 2018, 2019, 2016, 2017, 2018, 2016, 2017, 2018],
'Experience':[np.nan , np.nan, 3, np.nan, 2, np.nan, 0, 5, 0]
})
I am trying to back/forward fill the work experience (years) of employees. What I am trying to achieve is:
Employee 200
2019 - 3 yrs, 2018 - 2 yrs, 2017 - 1 yr
Employee 300
Keep as Nan
Employee 400
2018 - 3 yrs, 2017 - 2 yrs
Employee 500
2018 - 6 yrs, 2017 - 5 yrs, 2016 - 4 yrs
I am really struggling to get it to backfill (forwardfill) by increments of -1 (+1). Even trickier if the non-NaN/zero value is in the middle as in the case of employee 500.
df_test = pd.DataFrame({'DeptID':[0,0,0,1,1,1,2,2,2],
'Employee':[200, 200, 200, 300, 400, 400, 500, 500, 500],
'Year':[2017, 2018, 2019, 2016, 2017, 2018, 2016, 2017, 2018],
'Experience':[np.nan , np.nan, 3, np.nan, 2, np.nan, 0, 5, 0]
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设每个员工都有单个非零和非nan体验,请尝试以下
Assuming there's a single nonzero and non-nan experience for each employee, try this