Eri*_*c M 2 python pandas fillna
我有一个数据框,其中包含一列连续但不相邻的数字和缺失值。
我想使用该fillna函数用前一个非缺失行的递增值填充缺失值。
这是一个简化的表格:
index my_counter
0 1
1 2
2 NaN
3 3
4 NaN
5 NaN
6 8
Run Code Online (Sandbox Code Playgroud)
我想这样填写my_counter:
index my_counter
0 1
1 2
2 2.1
3 3
4 3.1
5 3.2
6 8
Run Code Online (Sandbox Code Playgroud)
我怎样才能完成这个任务?
IIUCffill与groupby 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2713 次 |
| 最近记录: |