首页 > 解决方案 > 根据其他列条件将当前值连接到先前值

问题描述

我是 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"

标签: python-3.x

解决方案


这是设置您的快速方法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这将创建包含DataFramein的变量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)

推荐阅读