- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个带有 MultiIndex 级别的 pandas DataFrame,如下所示:
现在我尝试将此数据帧转换为面向行的 JSON 对象(或 spreadsheet-style pivot table ?),结果应如下所示:
[{"date":"2016-11-08T23:00:00.000Z", "flag":0, "value":545.0, "moduleId":"module_A","parameterId":"parameter_B", "locationId":"location_1" },
{"date":"2016-11-09T23:00:00.000Z", "flag":0, "value":545.0, "moduleId":"module_A","parameterId":"parameter_B", "locationId":"location_1" },
{"date":"2016-11-10T23:00:00.000Z", "flag":8, "value":-999.0, "moduleId":"module_A","parameterId":"parameter_B", "locationId":"location_1" },
{"date":"2016-11-11T23:00:00.000Z", "flag":0, "value":680.0, "moduleId":"module_A","parameterId":"parameter_B", "locationId":"location_1" },
{"date":"2016-11-12T23:00:00.000Z", "flag":8, "value":-999.0, "moduleId":"module_A","parameterId":"parameter_B", "locationId":"location_1" }]
我尝试使用以下命令
df.reset_index().to_json(orient='records', date_format='iso')
但是 MultiIndex 结构保持不变。
上面使用的虚拟集可以按如下方式创建:
import pandas as pd
from pandas import Timestamp
input_dict = {('module_A',
'parameter_B',
'location_1',
'flag'): { Timestamp('2016-11-09 05:30:00+0630', tz='Asia/Yangon'): 0,
Timestamp('2016-11-10 05:30:00+0630', tz='Asia/Yangon'): 0,
Timestamp('2016-11-11 05:30:00+0630', tz='Asia/Yangon'): 8,
Timestamp('2016-11-12 05:30:00+0630', tz='Asia/Yangon'): 0,
Timestamp('2016-11-13 05:30:00+0630', tz='Asia/Yangon'): 8},
('module_A',
'parameter_B',
'location_1',
'value'): { Timestamp('2016-11-09 05:30:00+0630', tz='Asia/Yangon'): 545.0,
Timestamp('2016-11-10 05:30:00+0630', tz='Asia/Yangon'): 545.0,
Timestamp('2016-11-11 05:30:00+0630', tz='Asia/Yangon'): -999.0,
Timestamp('2016-11-12 05:30:00+0630', tz='Asia/Yangon'): 680.0,
Timestamp('2016-11-13 05:30:00+0630', tz='Asia/Yangon'): -999.0}}
df = pd.DataFrame(input_dict)
如有任何建议,我们将不胜感激。
最佳答案
您需要stack
+ rename_axis
第一:
df = df.stack([0,1,2]).rename_axis(['date','moduleId','parameterId','locationId'])
d = df.reset_index().to_json(orient='records', date_format='iso')
print (d)
[{"date":"2016-11-08T23:00:00.000Z","moduleId":"module_A","parameterId":"parameter_B","locationId":"location_1","flag":0,"value":545.0},
{"date":"2016-11-09T23:00:00.000Z","moduleId":"module_A","parameterId":"parameter_B","locationId":"location_1","flag":0,"value":545.0},
{"date":"2016-11-10T23:00:00.000Z","moduleId":"module_A","parameterId":"parameter_B","locationId":"location_1","flag":8,"value":-999.0},
{"date":"2016-11-11T23:00:00.000Z","moduleId":"module_A","parameterId":"parameter_B","locationId":"location_1","flag":0,"value":680.0},
{"date":"2016-11-12T23:00:00.000Z","moduleId":"module_A","parameterId":"parameter_B","locationId":"location_1","flag":8,"value":-999.0}]
关于python - 将 DataFrame 格式化为面向行的 JSON 对象(电子表格样式数据透视表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44136416/
我正在创建一个多区域 AlwaysOn Sql Availability 组。我在东部有 2 个虚拟机,在西部有 1 个虚拟机。我需要使用面向公众的负载平衡器对这些进行负载平衡。是否可以使用存在于不同
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我是 Smalltalk 技术的新手。我的编程经验是使用 C 和 C++。我想了解smalltalk的设计方法论。谁能推荐一些用smalltalk开发的简单的实时应用程序,可以帮助像我这样的初学者探索
现在我已经对 web2py 比较熟悉了,我想试试 Django。 主要区别是什么? 考虑到 web2py 知识,最有效的入门方法是什么? (一定有一些python应用框架的知识,不是吗?) 编辑 另外
我之前发过一个问题,Moving away from VSS ,其中我向使用 VSS 的 Delphi 开发人员询问了最佳的 VCS 控件。大多数开发人员似乎将 svn 与 TortoiseSVN 一
引用this question ,收集对 Delphi 初学者真正有帮助的网站链接可能会很好。 第一个答案指向Delphi Basics ,这是一个非常好的网站。 但一定还有更多。 所以:请为这个社区
我已经构建了一个 saas 产品,其中 Angular 4 与 golang Rest api 集成,并将构建上传到了 aws ec2 实例上。我的项目是一个基于 Multi-Tenancy 的应用程
我是 .NET 开发人员,在此之前使用过 VB6。我已经非常熟悉这些环境,并在垃圾收集语言的上下文中工作。但是,我现在希望通过 native C++ 增强我的技能,但发现自己有点不知所措。具有讽刺意味
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
由于我在数学方面没有任何出色的技能,所以我问您是否存在我应该在未来可能会改变的类(class)中使用的算法。 考虑以下场景: “角色”类具有以下字段: private boolean admin; p
我有许多面向“列”的 csv 文件,我需要对其进行预处理才能最终索引它们。 这是面向时间的数据,每个“设备”都有大量的列(最多 128 列),例如: LDEV_XXXXXX.csv
我想就重构技巧的最佳资源展开讨论,着眼于前端 JavaScript 重构。 一个我尊重其意见的 friend 建议这个 book ,尽管它使用 Java 中的示例。我明白 OO 重构的原则应该翻译成另
我不明白如何使用 Jquery/javascript 处理对象。 以及如何创建私有(private)方法/变量?我在论坛上看到关闭,但我尝试过但没有用。以及如何查看方法/变量是否是私有(private
我已经使用 MATLAB 多年并且非常熟练。我习惯了 MATLAB 调试,您可以在其中跳过行并查看变量及其变化方式,这就是我学习编码的方式。我现在正在研究使用 FORTRAN、C++ 和 Python
我正在学习如何实现“单一 Activity - 多个 fragment ”架构。据我所知,这是如今很普遍的方法,你可以找到很多关于这个主题的文章。例如这个 http://vinsol.com/blog
目前是否有任何简单的方法可以在 Apache Tomcat Servlet 容器前面设置 YAWS Web 服务器,类似于流行的 Apache httpd + Tomcat 设置? 最佳答案 我不相信
我是一个相当有能力的 Ruby 脚本编写者/程序员,但一直感到要扩展到 C++ 的压力。我没能找到任何与“C++ for Ruby Programmers”相关的网站。 This site Pytho
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 5 年前。
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我在表上有一个名为 cart_product_fti 的全文索引 每当我使用它时,我都必须准确列出索引中的所有字段: MATCH (foo,bar,etc) 我的想法是,如果稍后我决定向索引添加/删除
我是一名优秀的程序员,十分优秀!