简体   繁体   English

仅将两列从一个DataFrame复制到Pandas中的另一列

[英]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.

相关问题 使用 Pandas 将列从一个 DataFrame 复制到另一个 DataFrame 的最快方法? - Fastest way to copy columns from one DataFrame to another using pandas? Pandas:使用基于两列的另一个数据帧中的值替换一个数据帧中的值 - Pandas: replace values in one dataframe with values from another dataframe based on two columns 使用 .to_numpy() 将特定列从 Pandas Dataframe 的一行复制到另一行 - Using .to_numpy() to copy specific columns from one row of Pandas Dataframe to another 如何根据 pandas 中两个不同列的条件将值从一列复制到另一列? - how to copy values from one column into another column based on conditions of two different columns in pandas? 熊猫根据来自另一个数据库的多个列返回一个数据框的列 - Pandas return columns for one dataframe based on multple columns from another Pandas 将列名从一个数据帧复制到另一个 - Pandas copy column names from one dataframe to another 根据 Pandas 中的 id 将列值从一个数据帧复制到另一个数据帧 - Copy column value from one dataframe to another based on id in Pandas 尝试对2个pandas数据框进行排序,然后从一个复制到另一个 - Trying to sort 2 pandas dataframe and then copy from one to another 使用 pandas dataframe 上的名称将图像从一个文件夹复制到另一个文件夹 - Copy images from one folder to another using their names on a pandas dataframe 根据 Pandas 中的列值将内容从一个 Dataframe 复制到另一个 Dataframe - Copy contents from one Dataframe to another based on column values in Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM