根据其他列条件将当前值连接到先前值

时间:2019-01-19 04:08:44

标签: python-3.x

我是Pandas和Python的新手。我正在尝试使用Python模仿任务,类似于我在excel文件中创建的任务,以根据条件找到将当前值连接到以前的值

如果A =否,则B,否则B列中的当前值连接到B中的先前值

A         B          C
False     "bird"     "bird"
True      "fish"     "bird,fish"
True      "Tiger"    "bird,fish,Tiger"
False     "Elephant" "Elephant"

1 个答案:

答案 0 :(得分:0)

这是设置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的变量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)