Meta Xenology
Meta Xenology

Reputation: 142

Copy just two columns from one DataFrame to another in pandas

I have a DataFrame with shape of (418, 13) and I want to just copy the two columns into a new DataFrame for outputting to a csv file. (I am writing a prediction)

csv_pred = prediction[["PassengerId", "Survived"]].copy()
csv_pred.to_csv('n.csv')

However when I look into the outputted csv file I see this:

,PassengerId,Survived
0,892,0
1,893,1
2,894,0
. . .
. . . 

instead of what I expected which is:

"PassengerId","Survived"
892,0
893,1
894,0

Does anyone know why my code doesn't work? Thanks in advance.

Upvotes: 2

Views: 3268

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210832

There is no need to create a new DF:

prediction[["PassengerId", "Survived"]].to_csv('/path/to/file.csv', index=False)

Upvotes: 6

Related Questions