pandas fillna的值增加 [英] Pandas fillna with an incremented value

查看:83
本文介绍了 pandas fillna的值增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,其中有一列连续但不相邻的数字和缺失值.

I have a dataframe with a column of sequential but not adjacent numbers and missing values.

我想使用fillna函数用前一个非缺失行的增量值来填充缺失值.

I'd like to use the fillna function to fill in the missing values with an incremented value from the previous non-missing row.

这是一个简化的表:

index  my_counter
0      1
1      2
2      NaN
3      3
4      NaN
5      NaN
6      8

我想这样填写my_counter:

index  my_counter
0      1
1      2
2      2.1
3      3
4      3.1
5      3.2
6      8

我如何完成此任务?

推荐答案

IIUC ffillgroupby cumcount

df.my_counter.ffill()+df.groupby(df.my_counter.notnull().cumsum()).cumcount()/10
Out[92]: 
0    1.0
1    2.0
2    2.1
3    3.0
4    3.1
5    3.2
6    8.0
dtype: float64

这篇关于 pandas fillna的值增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆