如何将列名列表转换为 patsy 公式字符串?

How can I turn a list of column names into a patsy formula string?

我有一个 pandas 列名称列表(由所有虚拟变量组成),我想将其转换为公式字符串以复制并粘贴到 statsmodels。

有没有办法以编程方式执行此操作?

示例代码

list =    ['yrs_owned_model_28', 'yrs_owned_model_32', 'yrs_owned_model_35',
           'cm_ded_model_0', 'cm_ded_model_100', 'cm_ded_model_250',
           'cm_ded_model_500', 'cm_ded_model_750', 'cm_ded_model_1000',
           'cm_ded_model_2500']

期望的输出:

'yrs_owned_model_28 + yrs_owned_model_32 + yrs_owned_model_35 + cm_ded_model_0 + cm_ded_model_100 + cm_ded_model_250 + cm_ded_model_500 + cm_ded_model_750 + cm_ded_model_1000 + cm_ded_model_2500'
temp = ['yrs_owned_model_28', 'yrs_owned_model_32', 'yrs_owned_model_35',
           'cm_ded_model_0', 'cm_ded_model_100', 'cm_ded_model_250',
           'cm_ded_model_500', 'cm_ded_model_750', 'cm_ded_model_1000',
           'cm_ded_model_2500']

output = " + ".join(temp)
print(output)
temp = ['yrs_owned_model_28', 'yrs_owned_model_32', 'yrs_owned_model_35',
           'cm_ded_model_0', 'cm_ded_model_100', 'cm_ded_model_250',
           'cm_ded_model_500', 'cm_ded_model_750', 'cm_ded_model_1000',
           'cm_ded_model_2500']

output = ""
for col in temp:
    output += col
    output += ' + '

output = output[:-3]
print(output)