我有两个要合并的csv文件,方法是将一个csv的列信息添加到另一个csv。但是它们之间没有公共索引,但是它们确实具有相同数量的行(它们按顺序排列)。我已经看到了许多基于索引和相同编号加入csv文件的示例,但是我的csv文件没有相似的索引,但是顺序良好。我已经尝试了几个不成功的例子。
mycsvfile1
"a","1","mike"
"b","2","sally"
"c","3","derek"
mycsvfile2
"boy","63","retired"
"girl","55","employed"
"boy","22","student"
outcsvfile3所需的结果
"a","1","mike","boy","63","retired"
"b","2","sally","girl","55","employed"
"c","3","derek","boy","22","student"
代码:
import csv
import panada
df2 = pd.read_csv("mycsvfile1.csv",header=None)
df1 = pd.read_csv("mycsvfile2.csv", header=None)
df3 = pd.merge(df1,df2)
使用
df3 = pd.merge([df1,df2])
将数据添加到新行中对我没有帮助。非常感谢您的协助。
答案 0 :(得分:2)
如果两个数据框都具有编号索引(即,从0开始并以1递增-这是pd.read_csv
的默认行为),并假设两个DataFrame都已经按照正确的顺序排序,则行匹配,那么应该这样做:
df3 = pd.merge(df1,df2, left_index=True, right_index=True)
答案 1 :(得分:1)
除了columns
之外,df1
和df2
之间没有任何公用的index
。因此我们可以使用concat
pd.concat([df1,df2],axis=1)