python-3.x - 根据其他列条件将当前值连接到先前值
问题描述
我是 Pandas 和 Python 的新手。我正在尝试使用类似于我在 excel 文件中创建的 Python 来模拟任务,以根据条件将当前值连接到先前的值
如果 A = false 然后 B,否则 B 列中的当前值连接到 B 中的先前值
A B C
False "bird" "bird"
True "fish" "bird,fish"
True "Tiger" "bird,fish,Tiger"
False "Elephant" "Elephant"
解决方案
这是设置您的快速方法DataFrame
:
import pandas as pd
import numpy as np
data = [
[False, "bird", ""],
[True, "fish", ""],
[True, "Tiger", ""],
[False, "Elephant", ""],
]
df = pd.DataFrame(data=data, columns=["A", "B", "C"])
df
这将创建包含DataFrame
in的变量Pandas
。
现在,使用此代码遍历DataFrame
并设置每个值:
last = []
for index, row in df.iterrows():
if index == 0:
df.at[index, 'C'] = row['B'] # because first one has no previous to concatenate to
else:
if (row['A']): # check A
df.at[index, 'C'] = last['C']+','+row['B'] # if A is true, then concatenate previous B and this one
else:
df.at[index, 'C'] = row['B'] # else, use this B
last = row # now set this row to the last one that was accessed, for the next iteration of this loop
如果你print(pd)
在这一点上,你会得到预期的结果。
这是我使用的完整代码:
import pandas as pd
import numpy as np
data = [
[False, "bird", ""],
[True, "fish", ""],
[True, "Tiger", ""],
[False, "Elephant", ""],
]
df = pd.DataFrame(data=data, columns=["A", "B", "C"])
print(df)
last = []
for index, row in df.iterrows():
if index == 0:
df.at[index, 'C'] = row['B'] # because first one has no previous to concatenate to
else:
if (row['A']): # check A
df.at[index, 'C'] = last['B']+','+row['B']
else:
df.at[index, 'C'] = row['B']
last = row
print(df)
推荐阅读
- blockchain - 确定 BSC 卖出/买入交易
- .net-core - 运行 EF6 的问题“SqlQuery
()" 在 .NET Core 3.x 的上下文中 - http-status-code-400 - 如果请求值的数据类型不匹配,HTTP 状态码 400 或 422?
- url - 无法通过重定向加载页面
- powershell - Powershell远程处理是否递归序列化返回值?
- java - 找不到谁处理 AccessDeniedException
- r - 使用 gtsummary 中的 tbl_stack 时格式化 group_header 文本
- sql - 标量函数返回内存不足错误
- build - 从源错误构建虚幻引擎(针对 Hololens 问题.....)
- r - 检查 Snp 位置是否与基因的基因组区域共定位(开始和结束)