python - 如果当前行中某列的值为1,则将当前行替换为上一行的数据
问题描述
我有一个 pandas 数据框,如果当前行中某列的值为 1,则想用前一行的数据替换当前行,但还没有成功。任何帮助表示赞赏。
解决方案
可以这样做:
#B is the column that lets you know if the row should change or not
for i in range(1, len(df)):
if df.loc[i, 'B'] == 1:
df.loc[i, :] = df.loc[i-1, :]
推荐阅读
- symfony - 找不到如何为 Symfony 4 @Route 编写正确的正则表达式
- oracle - Oracle 返回不在 BYTE 中的列定义的正确大小
- google-apps-script - 无法从 Google 表格侧边栏下载文件
- grafana - 将两个标签合二为一作为 Grafana 变量
- node.js - vscode nodejs 远程调试 RPi
- reactjs - 如何在 React.FC 中使用泛型
- python - 有没有办法在 python 中获取单词中的字符数?
- mysql - 从一个表插入到另一个具有更多列的表
- java - Mockito“何时”方法与 Spring Boot 2 中 RestTemplateBuilder requestFactory 上的 lambda 参数不匹配
- javascript - 切换移动浏览器的地址栏时调整背景图像大小