简体   繁体   English

如何将熊猫数据帧附加到具有不同列的_csv

[英]How to append pandas dataframe to_csv with different columns

I have a pandas data frame read from csv file (data.csv). 我有一个从csv文件(data.csv)读取的熊猫数据框。 After processing data, I have another data frame less number of columns than in the data.csv file but still has same column name. 处理完数据后,我的另一个数据框的列数比data.csv文件中的少,但仍然具有相同的列名。 Now I want to append new data frame to data.csv file. 现在,我想将新的数据框附加到data.csv文件。
I don't know how to append and merge by column name. 我不知道如何按列名追加和合并。

data.csv file has structure data.csv文件具有结构

ID    name   email  
1     John   j@gmail.com  
2     Ann    a@gmail.com  

New data frame has structure 新数据框具有结构

ID    name  
1     Smith  
2     Kov  
3     Jane  

Now I want to write new data frame to data.csv file, result must be 现在我想将新的数据帧写入data.csv文件,结果必须是

ID     Name      email  
1      John      j@gmail.com  
2      Ann       a@gmail.com  
3      Smith   
4      Kov  
5      Jane  

The approach here isn't to append the existing csv, but rather to write over it, in this case with the original data as part of your output. 这里的方法不是附加现有的csv,而是覆盖它,在这种情况下,原始数据将作为输出的一部分。 If you read your original csv as df and your new dataframe is df2 : 如果您将原始csv读取为df而新数据框为df2

df3 = df.append(df2)
df3.to_csv("data.csv")

or on one line: 或一行:

df.append(df2).to_csv("data.csv")

The pd.DataFrame.append method works how you want it to when the column names match. 当列名称匹配时, pd.DataFrame.append方法pd.DataFrame.append您希望的方式工作。 Of course, I would recommend saving to another filename at first to make sure you're getting the output you want, and once you're happy with it changing your code to overwrite data.csv, lest you accidentally destroy data. 当然,我建议您先保存到另一个文件名,以确保获得所需的输出,一旦满意后,将代码更改为覆盖data.csv,以免意外删除数据。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM