0

I have an array of scores with values like

[[0.66372503]
 [0.64839758]
 [0.63307013]]

Then, I have an array of scores2 with values like

[[0.65367322]
 [0.63598164]
 [0.62295124]]

How do I combine them and save into csv like

         scores         scores2              
1     0.66372503     0.65367322
2     0.64839758     0.63598164
3     0.63307013     0.62295124

? Thank you

1
  • 2
    When you say "I have an array of...", is that a python array ? a NumPy nparray ? or a pandas DataFrame ? Please include the code that creates the data, so we can copy/paste it. Commented Jul 6, 2021 at 14:21

3 Answers 3

2
df= pd.DataFrame(data={'scores': [0.66372503, 0.64839758, 0.63307013],
                       'scores2':[0.65367322, 0.63598164, 0.62295124]})
df.to_csv('filename.csv', index=False)

if you have 2d array, you can use numpy flatten methode before passing the arrays into pandas df:

scores= [[0.66372503], [0.64839758], [0.63307013]]
scores2=[[0.65367322], [0.63598164], [0.62295124]]

df= pd.DataFrame(data={'scores': np.array(scores).flatten(),
                       'scores2':np.array(scores2).flatten()})

df.to_csv('filename.csv', index=False)
Sign up to request clarification or add additional context in comments.

2 Comments

Can you pass 2D lists as columns?
Yes, you can, but then each value would be a list with a single value, which I think won't be what you want.
0

if you have basic python arrays you could do something like this:

n = len(scores)
for x in range(0,n):
    key = x + 1
    newline = key + "\t" + scores[x] + "\t" + scores2[x] + "\n"
    outf.write(newline)

Comments

0

Try this:

scores = list(scores)
scores2 = list(scores2)
d = [scores, scores2]
export_data = zip_longest(*d, fillvalue = '')
with open('<your_file_name>.csv', 'w', encoding="ISO-8859-1", newline='') as myfile:
        wr = csv.writer(myfile)
        wr.writerow(("scores", "scores2"))
        wr.writerows(export_data)
    myfile.close()

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.