1
$\begingroup$

I have a $(418,2)$ matrix and I want to convert it to csv. so I write:

np.savetxt('titanic1.csv', Sol, fmt='%.2f', delimiter=",",header="PassengerId,Survived")

But the header I get is "# PassengerId" instead of "PassengerId" which is what I want. How can I solve this?

$\endgroup$
2
$\begingroup$

The documentation of numpy says you can add the parameter comments='', but warns that np.loadtxt() might not work on the resulting file.

$\endgroup$
2
  • 1
    $\begingroup$ Ok, I'll try it tomorrow morning. It's to upload the .csv file to kaggle. By the way I can't give you +1 because I don't have 15 reputation in this forum. Sorry. $\endgroup$ – elescararriba Jun 24 '19 at 21:38
  • $\begingroup$ No worries, I'm glad I could help. $\endgroup$ – Louic Jun 25 '19 at 7:45
0
$\begingroup$

You can replace your header with the following code:

for row in fileinput.input('titanic1.csv', inplace=True): 
    if fileinput.isfirstline(): 
        print('PassengerID,Survived') 
    else: 
        print(row)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.