[英]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. 我有一个形状为(418,13)的DataFrame,我只想将两列复制到一个新的DataFrame中以输出到一个csv文件。 (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: 但是,当我查看输出的csv文件时,会看到以下内容:
,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. 提前致谢。
无需创建新的DF:
prediction[["PassengerId", "Survived"]].to_csv('/path/to/file.csv', index=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.