首页 > 解决方案 > 如何附加具有不同列名的两个数据框并避免具有 nan 值的列

问题描述

xyarr= [[0,1,2],[1,1,3],[2,1,2]]
df1 = pd.DataFrame(xyarr, columns=['a', 'b','c'])

df2 = pd.DataFrame([['text','text2']], columns=['x','y'])

df3 = pd.concat([df1,df2],axis=0, ignore_index=True)

df3将具有NaN来自空列 ab c 的值。

     a    b    c     x      y
0  0.0  1.0  2.0   NaN    NaN
1  1.0  1.0  3.0   NaN    NaN
2  2.0  1.0  2.0   NaN    NaN
3  NaN  NaN  NaN  text  text2

我想保存df3到 csv,但没有额外的逗号有什么建议吗?

标签: pythonpandas

解决方案


As pd.concat is an outer join by default, you will get the NaN values from the empty columns a b c. If you use other Pandas function e.g. .join() which is left join by default, you can get around the problem here.

You can try using .join(), as follows:

df3 = df1.join(df2)

Result:

print(df3)

   a  b  c     x      y
0  0  1  2  text  text2
1  1  1  3   NaN    NaN
2  2  1  2   NaN    NaN

推荐阅读