python - 如何附加具有不同列名的两个数据框并避免具有 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,但没有额外的逗号有什么建议吗?
解决方案
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
推荐阅读
- c# - 如何从 ASP.NET Core 中的密码重置令牌中检索用户?
- html - 格式化表格有问题
- java - 将 spark 行转换为嵌套的 java 对象
- variables - 带有保留字符 % 的 Cmd 变量
- apache-spark - 在 Spark 中查询流式数据集
- asp.net-core - 控制器和 JsonResult asp.net 核心
- reactjs - 使用自定义快照序列化程序运行 Jest 快照测试时出错 - jest-styled-components
- java - 使用 openidConnectClient 功能的 WAS Liberty 中出现无效的 cookie 标头错误
- javascript - 从命令行捕获第三方站点的 HTML 画布作为图像
- android - 翻译,anuvaad karana 或 अनुवाद करना