0

我有一个 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'
4

1 回答 1

1
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)
于 2019-08-27T17:34:31.700 回答