如何根据列中的值计算新列?我需要创建字符串链以在 pandas python 中创建我的新列

How can I calculate a new column depending of the value from my column? I need to create string chains to create my new column in pandas python

我有一个名为 'big' 的列,其数值(15、...、28 等)被称为 'big',但根据这个数字,它应该将列与前 5 个数字的列名称相加,我意思是...

c15 c16 c17 ... c27 c28
23 1 0 1 ... 1 0
21 1 1 0 ... 1 1
... 0 0 1 ... 1 0
25 1 0 1 ... 1 1

因此,根据“大”列,例如 25,我的新列的总和应为 'c24'+'c23'+'c22'+'c21'+'c20' 并且结果必须在新的列名中计算。

试了好几个动作都不行。我在下面显示我的代码:

def test_fun(df):
    if (df['big'] > 19).all():
        pc = []
        for i in range(1,6):
            x = 'c' + (df['big'] - i).apply(str)
            pc.append(x)
        y = df[pc].sum(axis = 1)
        return y
    elif (df['big'] == 19).all():
        pc = []
        for i in range(1,5):
            x = 'c' + (df['big'] - i).apply(str)
            pc.append(x)
        y = df[pc].sum(axis = 1)
        return y
    elif (df['big'] == 18).all():
        pc = []
        for i in range(1,4):
            x = 'c' + (df['big'] - i).apply(str)
            pc.append(x)
        y = df[pc].sum(axis = 1)
        return y
    else:
        pc = []
        for i in range(1,3):
            x = 'c' + (df['big'] - i).apply(str)
            pc.append(x)
        y = df[pc].sum(axis = 1)
        return y

df['new_column'] = df.apply(lambda row: test_fun(df), axis = 1)

我添加了几个条件,因为实际上我的 table 是从 c15 到 c28 列开始的,但它会随着时间的推移而增加。

最后,当我使用函数 df.apply() 逐行应用我的函数时,我在试验期间遇到了几个错误。其中一些喜欢:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

这就是我在 if, elif, else 条件中添加的 .all()。甚至...

raise KeyError(f"None of [{key}] are in the [{axis_name}]")
KeyError: "None of [Index  'c27', 'c27', ...,\n   ('c26', 'c26',...  dtype='object')] are in the [columns]"

你知道我可能做错了什么吗?

使用pandas.DataFrame.apply的一种方式:

def get_big(series):
    n = series["big"]
    indices = ["c%s" % i for i in range(n-1, n-6, -1)]
    indices = series.index.intersection(indices)
    return series[indices].sum()

df.apply(get_big, axis=1)

示例数据

   c20  c21  c22  c23  c24  c25  c26  c27  c28  c29  big
0    0    1    1    0    1    0    1    1    1    0   21
1    1    0    1    0    0    0    1    0    1    0   28
2    1    1    0    1    0    1    0    1    0    0   20
3    0    0    0    0    1    0    0    1    0    1   20
4    1    1    0    1    0    0    0    0    0    0   23
5    1    0    0    1    0    0    0    1    0    0   25
6    0    1    0    0    1    1    1    0    1    0   23
7    1    0    1    0    0    0    0    1    0    1   20
8    1    0    1    0    1    1    0    0    0    1   26
9    0    0    0    1    1    0    1    1    0    1   25

输出:

0    0
1    1
2    0
3    0
4    2
5    2
6    1
7    0
8    3
9    2
dtype: int64