如何从另一列的值中正向填充一个列的零值?
我试图用“ end_daily_count”的上一个索引值填充列中的null值。启动数据集将是:
d = {
'id': [1, 1, 1, 1, 1, 2, 2, 2, 2],
'beginning_daily_count': [30, 33, 37, 46, None, 7, 1, None, 2],
'end_daily_count': [33, 37, 46, 52, 33, 7, 1, 2, 3],
'foils': [0, 0, 0, 0, 0, 0, 0, 1, 1]
}
所需的数据集将是:
d = {
'id': [1, 1, 1, 1, 1, 2, 2, 2, 2],
'beginning_daily_count': [30, 33, 37, 46, 52, 33, 1, 1, 2],
'end_daily_count': [33, 37, 46, 52, 33, 7, 1, 2, 3],
'foils': [0, 0, 0, 0, 0, 0, 0, 1, 1]
}
我尝试使用以下ffill()和iLoc()方法,但无济于事。诚然,我在FFILL和ILOC方面几乎没有经验。
d.iloc[beginning_daily_count.isna()].values = d.iloc[d.end_daily_count- 1].values
d['beginning_daily_count'].transform(lambda x: x.ffill(d['end_daily_count']))
I am trying to fill the null values within column 'beginning_daily_count' with the previous index value from the 'end_daily_count'. The starting dataset would be:
d = {
'id': [1, 1, 1, 1, 1, 2, 2, 2, 2],
'beginning_daily_count': [30, 33, 37, 46, None, 7, 1, None, 2],
'end_daily_count': [33, 37, 46, 52, 33, 7, 1, 2, 3],
'foils': [0, 0, 0, 0, 0, 0, 0, 1, 1]
}
and the desired dataset would be:
d = {
'id': [1, 1, 1, 1, 1, 2, 2, 2, 2],
'beginning_daily_count': [30, 33, 37, 46, 52, 33, 1, 1, 2],
'end_daily_count': [33, 37, 46, 52, 33, 7, 1, 2, 3],
'foils': [0, 0, 0, 0, 0, 0, 0, 1, 1]
}
I have attempted the following ffill() and iloc() methods, but to no avail. I admittedly have little experience with ffill and iloc.
d.iloc[beginning_daily_count.isna()].values = d.iloc[d.end_daily_count- 1].values
d['beginning_daily_count'].transform(lambda x: x.ffill(d['end_daily_count']))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
>方法可以接受系列作为其第一个参数,因此您可以将其传递给
end_daily_count
列的移位版本。假设您可以在不同的id
s上共享数据:The
DataFrame.fillna
method can accept a series as its first argument, so you can pass it a shifted version of yourend_daily_count
column. Assuming you are OK with potentially sharing data across differentid
s:您可以带有移位的其他列每组的列(使用
groupby.shiftby.shift
避免从一个组到下一个组泄漏值):输出:输出:
You can
fillna
the column with the shifted other column per group (usingGroupBy.shift
to avoid leaking values from one group to the next one):output:
这将查看以前的索引,并在设置better_daily_count替换时找到上一个'end_daily_count'
This will look at the previous index and find the 'end_daily_count' previous when the beginning_daily_count is set to replace