简体   繁体   English

如何将第一个 CSV 文件中特定列的数据提取到另一个 CSV 文件中的另一列?

[英]How to extract data from a specific column in the first CSV file to another column in another CSV file?

I have two different CSV files which i have imported using pd.read_csv .我有两个不同的 CSV 文件,它们是使用pd.read_csv导入的。

Both files have different header names.这两个文件具有不同的 header 名称。 I would like to export this specific column under the header name of ["Model"] in the first CSV file to the second CSV file under the header name of ["Product"]我想将第一个 CSV 文件中["Model"]的 header 名称下的这个特定列导出到["Product"]的 header 名称下的第二个 CSV 文件

I have tried using the following code but produced value error:我尝试使用以下代码但产生了值错误:

writer=df1[df1['Model']==df2['Product']]

Would appreciate any help.将不胜感激任何帮助。

Try joining the DataFrames on the index using pandas.DataFrame.join then exporting the result as a csv using pandas.DataFrame.to_csv .尝试使用 pandas.DataFrame.join 将数据帧加入索引,然后使用pandas.DataFrame.join将结果导出为pandas.DataFrame.to_csv

df1.join(df2)
df1.to_csv('./df2.csv')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将特定列从csv文件复制到特定位置的另一个csv - Copying a specific column from a csv file to another csv in a specific place Python 3,将特定列从一个csv文件复制到另一个csv - Python 3, Copy specific column from one csv file to another csv 如何基于特定列将csv文件中的特定行复制到另一个csv文件中? - How to copy specific rows from csv file to another csv file based on specific column? 如何从另一个 csv 文件添加列 - How to add column from another csv file 将特定的列从.csv文件复制到另一个.csv文件,并将其另存为新的.csv文件问题 - Copy specific Column from a .csv file to another .csv file and save it as a new .csv file issue 从具有指定行名和列名的 csv 文件中提取特定数据 - extract specific data from a csv file with specified row and column names Python - 查找 CSV 中的列的平均值,给定另一列中的值(来自具有多年的文件中特定年份的数据)? - Python - Finding average of a column in a CSV given a value in another column (data from a specific year in a file with multiple years)? 将一个 CSV 文件中的列添加到另一个 CSV 文件 - Adding column from one CSV file to another CSV file 从另一个 csv 文件替换一个 csv 文件的列 - replacing the column of one csv file from another csv file 使用来自另一个csv文件的列创建一个新的csv文件 - Create a new csv file with a column from another csv file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM