如何在python中定义可变数量的列

如何在python中定义可变数量的列,python,pandas,multiple-columns,apply,Python,Pandas,Multiple Columns,Apply,我正在尝试使用apply函数向python中添加列。 但是,要添加的列数取决于函数的输出 在应用函数中使用 示例代码: number_of_columns_to_be_added = 2 def add_columns(number_of_columns_to_be_added): df['n1'],df['n2'] = zip(*df['input'].apply(lambda x : do_something(x, number_of_columns_to_be_a

我正在尝试使用apply函数向python中添加列。 但是,要添加的列数取决于函数的输出 在应用函数中使用

示例代码:

number_of_columns_to_be_added = 2    
def add_columns(number_of_columns_to_be_added):
         df['n1'],df['n2'] = zip(*df['input'].apply(lambda x : do_something(x, number_of_columns_to_be_added)))

关于如何在
=zip(…
部分编程之前定义丑陋的列部分
(df['n1'],…,df['n696969'])
有什么想法吗?

我猜
zip
的输出是一个元组,因此您可以尝试以下方法:

temp = zip(*df['input'].apply(lambda x : do_something(x, number_of_columns_to_be_added)))
for i, value in enumerate(temp, 1):
    key = 'n'+str(i)
    df[key] = value
temp
将保存所有条目,然后您对
temp
进行迭代,以使用特定键将值分配给dict。希望这符合您的原始想法