pythonstringlistpatsy

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


I have a list of pandas column names (consisting of all dummy variables) that I would like to turn into a formula string to copy and paste for statsmodels.

Is there a way to programmatically do this?

Example code

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']

Desired output:

'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'

Solution

  • 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)