根据密钥和辅助密钥合并两个csv文件

时间:2016-06-11 13:42:18

标签: python csv

我想合并两个csv文件,如下所示:

csv1:

formula,solver,runtime,conflicts
CBS_k3_n100_m403_b30_13.cnf,SWDiA5BY,0.001842,318
CBS_k3_n100_m403_b30_13.cnf,glucose,0.001842,318

CSV2:

formula,entropy,num sols
CBS_k3_n100_m403_b30_13.cnf,0.202,707286

所需的输出:

formula,solver,runtime,conflicts,entropy,solutions
CBS_k3_n100_m403_b30_13.cnf,SWDiA5BY,0.001842,318,0.202,707286
CBS_k3_n100_m403_b30_13.cnf,glucose,0.001842,318,0.202,707286

所以我在两个词典(csv's)的键之间做了一个交集,并使用了列表理解

keysA = set(dict1.keys())
keysB = set(dict2.keys())
keys = keysA & keysB
...
[[key] + dict1.get(key, []) + dict2.get(key, []) for key in keys]

但有一些'重复'行(我需要),其中字段公式相同但字段求解器不是,我的输出是:

formula,solver,runtime,conflicts,entropy,solutions
CBS_k3_n100_m403_b30_13.cnf,SWDiA5BY,0.001842,318,0.202,707286

如何使用列表理解保留这些行?或以任何其他方式

感谢您的帮助

编辑 - 添加了一个例子

1 个答案:

答案 0 :(得分:2)

你为什么不用熊猫?在熊猫中很容易做到这一点

import pandas as pd
df1=pd.read_csv("1.csv")
df=pd.read_csv("2.csv")
result=df1.merge(df,on="formula")
result.to_csv("result.csv")

你也可以使用result=df1.merge(df,on="formula",how="outer")来保留你的csv有一个但其他没有的公式