1.02.03
我有以下数据框架,我想应用bfill如下:
"百分比"根据NaN
s创建组
df['group_id'] = df.amount.where(df.amount.isna(), 1).cumsum().bfill()
创建填充函数
def custom_fill(x):
# Calculate number of rows to be filled
max_fill_rows = math.floor(x.iloc[-1, 1] * (x.shape[0] - 1) / 100)
# Fill only if number of rows to fill is not zero
return x.bfill(limit=max_fill_rows) if max_fill_rows else x
填充DataFrame
df.groupby('group_id').apply(custom_fill)
输出amount percentage group_id
0 NaN NaN 1.0
1 1.0 20.0 1.0
2 2.0 10.0 2.0
3 NaN NaN 3.0
4 NaN NaN 3.0
5 3.0 50.0 3.0
6 3.0 50.0 3.0
7 3.0 50.0 3.0
8 4.0 10.0 4.0
9 NaN NaN 5.0
10 5.0 10.0 5.0
PS:不要忘记导入所需的库
import math
相关内容
- Python条件组合
- 基于DivIcon的Python条件class_name
- 从符合数据框架Python条件的列表中索引并保存最后N个点
- Python:条件类参数
- 为什么我的Python条件内函数不工作?
- Pyathena SQL查询与Python条件
- 用于字符串值着色的Python条件格式
- python条件格式化到excel文件
- Python -条件中的语法无效
- 根据Python条件从列表中过滤字典
- 我如何使用python条件映射列在一个数据框重复在他们?
- 用python条件过滤嵌套列表
- 不带if语句的Python条件语句
- 创建一个列表,通过索引的列值满足python条件
- Python条件语句中的一个变量
- 如何将3个python条件与1个其他条件一起使用
- 在表列上使用Python条件创建列表
- Python条件触发两次
- python条件组合
- Python条件拆分
最新更新
- Textarea Label in Form: Error: Label元素的for属性的值必须是一个非隐藏表单控件的I
- 如何在没有源文件的情况下使用XSL创建XML
- 通过指针在实例上实现类型的调用函数
- 在批处理脚本中替换而不是添加前缀
- 如果类没有初始化,c++编译器会生成一个defctor吗?
- 在我的代码中没有得到任何方程的答案,我的按钮进入我的字段
- vsto Outlook插件在Outlook 2016和Office 365中的行为不同
- npm install不会在我的React项目中安装任何node模块
- 用两次检查遍历两个列表
- 将图像转换为RGB格式的文本文件,格式为三逗号分隔
- 我正试图找到一种方法将TimeSpan变量转换为秒的整数量
- FCM云消息传递验证令牌
- C多重间接
- 显示数组中元素的倒计时。setInterval 不会导致延迟。最后一个元素立即显示
- sveltekit:等待来自+服务器的数据.t加载函数
- JQuery是:检查每次点击返回两个值
- RxJS中具有共享结果的请求队列
- Beautifulsoup如何添加一些东西到列,如果元素没有显示
- 在Jekyll blog中按2D位置(距离)搜索
- Robocopy在复制过程中向文件名添加日期
- c -间接递归的用例
- 如何将日期对象转换为格式(DD-MM-YYYY)
- 如何从没有使用python硒链接的网站下载文件?
- 用错误处理循环捕获用户输入的最佳方法
- 如何使用内部连接在php发送值从下拉菜单到数据库的两个不同的表
- 我如何得到一个状态的IISreset - true,如果它是启动和false,如果它是停止
- 如何复制列标题"Testing"的列
- 读取文件夹中的多个DOCX文件到多个dataframe时出错
- 无法下载InceptionV3 Keras
- 如何打印数据在模板django的一个不同的表由外键连接?
热门标签:
javascript python java c# php android html jquery c++ css ios sql mysql arrays asp.net json python-3.x ruby-on-rails .net sql-server django objective-c excel regex ruby linux ajax iphone xml vba spring asp.net-mvc database wordpress string postgresql wpf windows xcode bash git oracle list vb.net multithreading eclipse algorithm macos powershell visual-studio image forms numpy scala function api selenium