0

I know this question was already asked, but after trying most of the accepted answer none of them seems to work with my simple task...

I have a csv file as follow:

Date,Median
2000-01-31,9
2000-02-28,8
2000-03-31,7
2000-04-30,6
2000-05-31,5
2000-06-30,4
2000-07-31,3
2000-08-31,2
2000-09-30,1
2000-10-31,0
2000-11-30,11
2000-12-31,12

and then an array:

[0.1829   0.171349 0.162461 0.152306 0.14122  0.137749 0.138802 0.150315
 0.156784 0.168297 0.180634 0.187241]

I wish to append this array as a third column to the csv file to get the following output:

Date,Median,Median2
2000-01-31,9,0.1829
2000-02-28,8,0.171349
2000-03-31,7,0.162461
2000-04-30,6,0.152306
2000-05-31,5,0.14122
2000-06-30,4,0.137749
2000-07-31,3,0.138802
2000-08-31,2,0.150315
2000-09-30,1,0.156784
2000-10-31,0,0.168297
2000-11-30,11,0.180634
2000-12-31,12,0.187241

I tried most of the answer related to this kind of question but I did not succeed to make them work..here is the last code I tried, using pandas that looks easier but it does not work:

data=pd.read_csv("data_1.csv",sep=',')
array_transpose = array.reshape((-1, 1)) #in order to transpose the array
data['Median2'] = data[array_transpose]
data.to_csv('output.csv')

which produce the following error:

KeyError: '[0.1829   0.171349 0.162461 0.152306 0.14122  0.137749 0.138802   0.150315\n 0.156784 0.168297 0.180634 0.187241] not in index'

How to append this array to my csv file?

1 Answer 1

1

You may not need reshape

data=pd.read_csv("data_1.csv",sep=',')
data['Median2'] = array
data.to_csv('output.csv')
Sign up to request clarification or add additional context in comments.

Comments

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.