如何逐行合并csv文件python

如何逐行合并csv文件python,python,python-3.x,pandas,csv,merge,Python,Python 3.x,Pandas,Csv,Merge,您好,我是Python新手,我正在尝试使用这段代码将这两个文件fileA和fileB合并为一个文件 a = pandas.read_csv(fileA) b = pandas.read_csv(fileB) b = b.dropna(axis=1) merged = b.merge(a, on="day") merged.to_csv(output, index=False) 但问题不是逐行合并,而是将文件A的第一行与文件B的所有行合并!下面是一个例子

您好,我是Python新手,我正在尝试使用这段代码将这两个文件fileA和fileB合并为一个文件

    a = pandas.read_csv(fileA)
    b = pandas.read_csv(fileB)
    b = b.dropna(axis=1)
    merged = b.merge(a, on="day")
    merged.to_csv(output, index=False)
但问题不是逐行合并,而是将文件A的第一行与文件B的所有行合并!下面是一个例子

文件的内容a

numb,day

one,sat
two,sun  
three,mon
文件内容b

day,month,color,shape

sat,apr,black,circle
sun,may,white,triangle
mon,jun,red,square
预期产出

numb,day,month,color,shape

one,sat,apr,black,circle
two,sun,may,white,triangle
three,mon,jun,red,square
我实际得到的

numb,day,month,color,shape

one,sat,apr,black,circle
one,sat,may,white,triangle
one,sat,mon,jun,red,square
two,sun,apr,black,circle
two,sun,may,white,triangle
two,sun,jun,red,square
.
.
.
那么,我怎样才能一行一行地合并文件,而不是所有这些,或者我到底做错了什么

我正在使用Python 3.7来组合数据帧:

a = pandas.read_csv(fileA)
b = pandas.read_csv(fileB)
b = b.dropna(axis=1)

merged = pd.concat([a, b], axis=1)

merged.to_csv('output.csv', index=False)
用于组合数据帧:

a = pandas.read_csv(fileA)
b = pandas.read_csv(fileB)
b = b.dropna(axis=1)

merged = pd.concat([a, b], axis=1)

merged.to_csv('output.csv', index=False)
你可以用

你可以用


试试看:
df_out=pd.concat([a,b],axis=1)
这能回答你的问题吗?事实上,
concat
是一条路要走。您使用了
merge
,这有点像在引擎盖下进行连接,因此产生了不希望的结果。@chrisA concat对csv文件起作用吗?哦,我明白了,现在我想我必须更改我已经使用的函数。非常感谢@ChrisAtry:
df_out=pd.concat([a,b],axis=1)
这能回答你的问题吗?事实上,
concat
是一条路要走。您使用了
merge
,这有点像在引擎盖下进行连接,因此产生了不希望的结果。@chrisA concat对csv文件起作用吗?哦,我明白了,现在我想我必须更改我已经使用的函数。这很有效,非常感谢你@ChrisA