Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 合并具有相同标题名的列_Python_Pandas - Fatal编程技术网

Python 合并具有相同标题名的列

Python 合并具有相同标题名的列,python,pandas,Python,Pandas,假设我有两个dfs: df1= df2= 我希望df1是: index colx coly colz colf 0 45 35 54 nan 1 22 nan 11 5 我正在研究合并和加入,但我似乎不能正确地完成它 谢谢您可以加入转置的数据帧,再次转置结果,并添加默认数字索引: df1.T.join(df2.T, rsuffix=

假设我有两个dfs: df1=

df2=

我希望df1是:

index      colx      coly      colz      colf  
  0         45        35        54       nan
  1         22        nan       11        5
我正在研究合并和加入,但我似乎不能正确地完成它


谢谢

您可以加入转置的数据帧,再次转置结果,并添加默认数字索引:

df1.T.join(df2.T, rsuffix='r').T.reset_index(drop=True)
#       colx  coly  colz
#0      45.0  35.0  54.0
#1      22.0   NaN  11.0
使用
pd.concat
使用
pd.merge

你是说熊猫吗?如果是这样,请将pandas添加为一个标记,然后共享您迄今为止尝试过的代码。第二个数据帧中没有
coly
,为什么在输出中有它?谢谢@DYZ。这是因为我没有在结果中添加colf。我现在就纠正它。
colg
怎么了?
index      colx      coly      colz      colf  
  0         45        35        54       nan
  1         22        nan       11        5
df1.T.join(df2.T, rsuffix='r').T.reset_index(drop=True)
#       colx  coly  colz
#0      45.0  35.0  54.0
#1      22.0   NaN  11.0
pd.concat([df, df2], sort=False)[df.set_index('index').columns].reset_index(drop=True)

    colx    coly    colz
0   45      35.0    54
1   22      NaN     11
pd.merge(df,df2, how='outer')[df.set_index('index').columns]

    colx    coly    colz
0   45      35.0    54
1   22      NaN     11