0

I have an array of response variables which I have got using regression analysis by using further code.

SalePrice_baseline = reg2.predict(test)

The output looks like,

array([[ 103612.29783843],
       [  95608.74582384],
       [ 178230.07228516],
       ..., 
       [ 172559.98073767],
       [ 121881.65675305],
       [ 218179.82985471]])

I have an existing test csv file of all the input variables as test.csv. I want to add this array as a new column.

How do I do that?

Thank you.

1
  • read all columns from csv file, add column to columns in memory, write all columns to csv file. Commented Nov 29, 2016 at 21:08

1 Answer 1

1

If the CSV file has the same number of lines as your vector, then you can read a line as a string, then print it plus a comma plus the corresponding vector value

with open('a.csv') as inp:
    with open('b.csv', 'w') as out:
        for line, value in zip(inp, vector)
            line = line.rstrip()
            print(line, ',', value, sep='', file=out)
Sign up to request clarification or add additional context in comments.

1 Comment

You could also use the built-in csv writer as in the other answer with this algorithm. It will auto-escape values, allow you to easily change separators, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.