python - Python 3.x:从两个不同形状的字典创建数据框
问题描述
与此处已发布的内容类似Create dataframe from two dictionaries。所有键都存在于两个字典中,唯一的区别是数组值的形状。
我有的:
d1 = {(1, "Autumn"): np.array([[2.5, 100], [4.5, 105], [7.5, 120], [9.5,137]]), (1, "Spring"): np.array([[10.5, 146], [11.7, 151],
[12.3, 164], [15.0, 173]])}
d2 = {(1, "Autumn"): np.array([10.2, 13.3, 15.7, 18.8]), (1, "Spring"): np.array([15.6, 20, 23, 27])}
要达到的目标:
d3 = {(1, "Autumn"): pd.DataFrame([[2.5, 100, 10.2], [4.5, 105, 13.3], [7.5, 120, 15.7], [9.5, 137, 18.8]],
columns = ["x", "y", "z"]), (1, "Spring"): pd.DataFrame([[10.5, 146, 15.6], [11.7, 151, 20], [12.3, 164, 23], [15.0, 173, 27]],
columns = ["x", "y", "z"])}
你能帮帮我吗?我尝试使用作为答案发布的方法。但是,我得到ValueError: could not broadcast input array from shape (4,2) into shape (4)
错误。
解决方案
推荐阅读
- ios - 为什么应用程序组在我的 iOS 项目中显示为红色?
- python - 用于密码学的 Python 异步模块?
- html - 我想将弹出文本居中,使其位于中间……但我不能?
- javascript - 尝试修改文件内容时,MathJax 无法正常工作
标签
- html - 如何在我的手机和电脑网页中适应代码中显示的表格
- html - CSS媒体查询没有显示任何效果
- javascript - 反应原生。在反应导航中隐藏来自 BottomTab.Navigator 的项目
- android - 在导航组件中导航后的 android-clear 片段
- python - 在 Python 中过滤对象数组
- node.js - 在管理控制台中使用电子邮件和密码创建用户会导致匿名用户